summaryrefslogtreecommitdiff
path: root/py
diff options
context:
space:
mode:
Diffstat (limited to 'py')
-rw-r--r--py/chat.py31
-rw-r--r--py/complete.py29
2 files changed, 36 insertions, 24 deletions
diff --git a/py/chat.py b/py/chat.py
index 4c3d532..b6dcd35 100644
--- a/py/chat.py
+++ b/py/chat.py
@@ -1,8 +1,8 @@
-import openai
-
+import requests
import sys
import os
-import openai
+
+file_content = vim.eval("prompt")
config_file_path = os.path.join(os.path.expanduser("~"), ".config/openai.token")
@@ -14,12 +14,9 @@ try:
except Exception:
pass
-openai.api_key = api_key.strip()
-
-lines = sys.stdin.readlines()
-
-file_content = "".join(lines)
+api_key = api_key.strip()
+lines = file_content.splitlines()
messages = []
for line in lines:
@@ -40,11 +37,19 @@ if not messages:
file_content = ">>> user\n\n" + file_content
messages.append({"role": "user", "content": file_content })
-response = openai.ChatCompletion.create(
- model="gpt-3.5-turbo",
- messages=messages
-)
+
+url = "https://api.openai.com/v1/chat/completions"
+headers = {
+ 'Content-Type': 'application/json',
+ 'Authorization': F"Bearer {api_key}"
+}
+data = {
+ "model": "gpt-3.5-turbo",
+ "messages": messages
+}
+response = requests.post(url, headers=headers, json=data)
+response = response.json()
answer = response['choices'][0]['message']['content']
-print(f"{file_content.strip()}\n\n<<< assistant\n\n{answer.strip()}\n\n>>> user\n")
+output = f"{file_content.strip()}\n\n<<< assistant\n\n{answer.strip()}\n\n>>> user\n"
diff --git a/py/complete.py b/py/complete.py
index ba460c4..409d47f 100644
--- a/py/complete.py
+++ b/py/complete.py
@@ -1,6 +1,8 @@
+import requests
import sys
import os
-import openai
+
+prompt = vim.eval("prompt")
config_file_path = os.path.join(os.path.expanduser("~"), ".config/openai.token")
@@ -12,15 +14,20 @@ try:
except Exception:
pass
-openai.api_key = api_key.strip()
-
-prompt = "".join(sys.stdin.readlines())
+api_key = api_key.strip()
-completion = openai.Completion.create(
- model="text-davinci-003",
- prompt=prompt,
- max_tokens=1000,
- temperature=0.1
-)
+url = "https://api.openai.com/v1/completions"
+headers = {
+ 'Content-Type': 'application/json',
+ 'Authorization': f"Bearer {api_key}"
+}
+data = {
+ "model": "text-davinci-003",
+ "prompt":prompt,
+ "max_tokens": 1000,
+ "temperature": 0.1
+}
+response = requests.post(url, headers=headers, json=data)
+response = response.json()
-print(completion.choices[0].text)
+output = response['choices'][0]['text']