From 882ac0476b71642694e764d81df2fb215238c65d Mon Sep 17 00:00:00 2001 From: Martin Bielik Date: Mon, 13 Mar 2023 18:07:01 +0100 Subject: chat streaming, more py3 integration --- py/complete.py | 39 ++++++++++++--------------------------- 1 file changed, 12 insertions(+), 27 deletions(-) (limited to 'py/complete.py') diff --git a/py/complete.py b/py/complete.py index 409d47f..c37cbd0 100644 --- a/py/complete.py +++ b/py/complete.py @@ -1,33 +1,18 @@ -import requests -import sys -import os +import openai -prompt = vim.eval("prompt") - -config_file_path = os.path.join(os.path.expanduser("~"), ".config/openai.token") +# import utils +plugin_root = vim.eval("s:plugin_root") +vim.command(f"py3file {plugin_root}/py/utils.py") -api_key = os.getenv("OPENAI_API_KEY") - -try: - with open(config_file_path, 'r') as file: - api_key = file.read() -except Exception: - pass +prompt = vim.eval("prompt") -api_key = api_key.strip() +openai.api_key = load_api_key() -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() +response = openai.Completion.create( + model="text-davinci-003", + prompt=prompt, + max_tokens=1000, + temperature=0.1 +) output = response['choices'][0]['text'] -- cgit v1.2.3