summaryrefslogtreecommitdiffstats
path: root/gpt4free
diff options
context:
space:
mode:
authorHexye <65314629+HexyeDEV@users.noreply.github.com>2023-05-01 19:24:01 +0200
committerGitHub <noreply@github.com>2023-05-01 19:24:01 +0200
commit611a565092e5ca22d76f6d2c9481a2f89031ea2f (patch)
tree2d9a4ce812960a738636212e06e783bda8e4b1d3 /gpt4free
parentMerge pull request #332 from mache102/main (diff)
downloadgpt4free-611a565092e5ca22d76f6d2c9481a2f89031ea2f.tar
gpt4free-611a565092e5ca22d76f6d2c9481a2f89031ea2f.tar.gz
gpt4free-611a565092e5ca22d76f6d2c9481a2f89031ea2f.tar.bz2
gpt4free-611a565092e5ca22d76f6d2c9481a2f89031ea2f.tar.lz
gpt4free-611a565092e5ca22d76f6d2c9481a2f89031ea2f.tar.xz
gpt4free-611a565092e5ca22d76f6d2c9481a2f89031ea2f.tar.zst
gpt4free-611a565092e5ca22d76f6d2c9481a2f89031ea2f.zip
Diffstat (limited to 'gpt4free')
-rw-r--r--gpt4free/italygpt/README.md18
-rw-r--r--gpt4free/italygpt/__init__.py28
2 files changed, 46 insertions, 0 deletions
diff --git a/gpt4free/italygpt/README.md b/gpt4free/italygpt/README.md
new file mode 100644
index 00000000..984eff3a
--- /dev/null
+++ b/gpt4free/italygpt/README.md
@@ -0,0 +1,18 @@
+### Example: `italygpt`
+
+```python
+# create an instance
+from gpt4free import italygpt
+italygpt = italygpt.Completion()
+
+# initialize api
+italygpt.init()
+
+# get an answer
+italygpt.create(prompt="What is the meaning of life?")
+print(italygpt.answer) # html formatted
+
+# keep the old conversation
+italygpt.create(prompt="Are you a human?", messages=italygpt.messages)
+print(italygpt.answer)
+``` \ No newline at end of file
diff --git a/gpt4free/italygpt/__init__.py b/gpt4free/italygpt/__init__.py
new file mode 100644
index 00000000..27a965f1
--- /dev/null
+++ b/gpt4free/italygpt/__init__.py
@@ -0,0 +1,28 @@
+import requests, time, ast, json
+from bs4 import BeautifulSoup
+from hashlib import sha256
+
+class Completion:
+ # answer is returned with html formatting
+ next_id = None
+ messages = []
+ answer = None
+
+ def init(self):
+ r = requests.get("https://italygpt.it")
+ soup = BeautifulSoup(r.text, "html.parser")
+ self.next_id = soup.find("input", {"name": "next_id"})["value"]
+
+ def create(self, prompt: str, messages: list = []):
+ try:
+ r = requests.get("https://italygpt.it/question", params={"hash": sha256(self.next_id.encode()).hexdigest(), "prompt": prompt, "raw_messages": json.dumps(messages)}).json()
+ except:
+ r = requests.get("https://italygpt.it/question", params={"hash": sha256(self.next_id.encode()).hexdigest(), "prompt": prompt, "raw_messages": json.dumps(messages)}).text
+ if "too many requests" in r.lower():
+ # rate limit is 17 requests per 1 minute
+ time.sleep(20)
+ return self.create(prompt, messages)
+ self.next_id = r["next_id"]
+ self.messages = ast.literal_eval(r["raw_messages"])
+ self.answer = r["response"]
+ return self \ No newline at end of file