Skip to content

Commit 0537198

Browse files
committed
upload italtgpt2
1 parent b6efc1b commit 0537198

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed

gpt4free/italygpt2/README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Itagpt2(Rewrite)
2+
Written by [sife-shuo](https://github.com/sife-shuo/).
3+
4+
## Description
5+
Unlike gpt4free. italygpt in the pypi package, italygpt2 supports stream calls and has changed the request sending method to enable continuous and logical conversations.
6+
7+
The speed will increase when calling the conversation multiple times.
8+
9+
### Completion:
10+
```python
11+
account_data=italygpt2.Account.create()
12+
for chunk in italygpt2.Completion.create(account_data=account_data,prompt="Who are you?"):
13+
print(chunk, end="", flush=True)
14+
print()
15+
```
16+
17+
### Chat
18+
Like most chatgpt projects, format is supported.
19+
Use the same format for the messages as you would for the [official OpenAI API](https://platform.openai.com/docs/guides/chat/introduction).
20+
```python
21+
messages = [
22+
{"role": "system", "content": ""},#...
23+
{"role": "user", "content": ""}#....
24+
]
25+
account_data=italygpt2.Account.create()
26+
for chunk in italygpt2.Completion.create(account_data=account_data,prompt="Who are you?",message=messages):
27+
print(chunk, end="", flush=True)
28+
print()
29+
```

gpt4free/italygpt2/__init__.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import re
2+
import requests
3+
import hashlib
4+
from fake_useragent import UserAgent
5+
class Account:
6+
@staticmethod
7+
def create():
8+
r=requests.get("https://italygpt.it/",headers=Account._header)
9+
f=r.text
10+
tid=re.search('<input type=\"hidden\" name=\"next_id\" id=\"next_id\" value=\"(\w+)\">',f).group(1)
11+
if len(tid)==0:
12+
raise RuntimeError("NetWorkError:failed to get id.")
13+
else:
14+
Account._tid=tid
15+
Account._raw="[]"
16+
return Account
17+
def next(next_id:str)->str:
18+
Account._tid=next_id
19+
return Account._tid
20+
def get()->str:
21+
return Account._tid
22+
_header={
23+
"Host": "italygpt.it",
24+
"Referer":"https://italygpt.it/",
25+
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",#UserAgent().random,
26+
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
27+
"Accept-Language":"zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
28+
"Upgrade-Insecure-Requests":"1",
29+
"Sec-Fetch-Dest":"document",
30+
"Sec-Fetch-Mode":"navigate",
31+
"Sec-Fetch-Site":"none",
32+
"Sec-Fetch-User":"?1",
33+
"Connection":"keep-alive",
34+
"Alt-Used":"italygpt.it",
35+
"Pragma":"no-cache",
36+
"Cache-Control":"no-cache",
37+
"TE": "trailers"
38+
}
39+
def settraw(raws:str):
40+
Account._raw=raws
41+
return Account._raw
42+
def gettraw():
43+
return Account._raw
44+
45+
class Completion:
46+
@staticmethod
47+
def create(
48+
account_data,
49+
prompt: str,
50+
message=False
51+
):
52+
param={
53+
"prompt":prompt.replace(" ","+"),
54+
"creative":"off",
55+
"internet":"false",
56+
"detailed":"off",
57+
"current_id":"0",
58+
"code":"",
59+
"gpt4":"false",
60+
"raw_messages":account_data.gettraw(),
61+
"hash":hashlib.sha256(account_data.get().encode()).hexdigest()
62+
}
63+
if(message):
64+
param["raw_messages"]=str(message)
65+
r = requests.get("https://italygpt.it/question",headers=account_data._header,params=param,stream=True)
66+
account_data.next(r.headers["Next_id"])
67+
account_data.settraw(r.headers["Raw_messages"])
68+
for chunk in r.iter_content(chunk_size=None):
69+
r.raise_for_status()
70+
yield chunk.decode()

testing/italygpt2_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from gpt4free import italygpt2
2+
account_data=italygpt2.Account.create()
3+
for chunk in italygpt2.Completion.create(account_data=account_data,prompt="Who are you?"):
4+
print(chunk, end="", flush=True)

0 commit comments

Comments
 (0)