diff --git a/.grit/grit.yaml b/.grit/grit.yaml new file mode 100644 index 0000000..e765edb --- /dev/null +++ b/.grit/grit.yaml @@ -0,0 +1,8 @@ +version: 0.0.1 +patterns: + - name: github.com/getgrit/js#* + - name: github.com/getgrit/python#* + - name: github.com/getgrit/json#* + - name: github.com/getgrit/hcl#* + - name: github.com/getgrit/python#openai + level: info diff --git a/config.py b/config.py index e7e0fdf..c12e4ee 100644 --- a/config.py +++ b/config.py @@ -63,7 +63,7 @@ def int_env(name, default): logger.info(f'Use transformer model {transformer_model}') openai_keys = os.getenv('OPENAI_API_KEY').split(',') if os.getenv('OPENAI_API_KEY') else [None] -openai.api_key = random.choice(openai_keys) # Round-robin available keys +raise Exception("The 'openai.api_key' option isn't read in the client API. You will need to pass it when you instantiate the client, e.g. 'OpenAI(api_key=random.choice(openai_keys))'") # Round-robin available keys logger.info(f'Use openai api key #{openai_keys.index(openai.api_key)}') openai_model = os.getenv('OPENAI_MODEL') or 'gpt-3.5-turbo' openai_score_threshold = int_env('OPENAI_SCORE_THRESHOLD', 20) diff --git a/hacker_news/news.py b/hacker_news/news.py index 84f2a40..fab4084 100644 --- a/hacker_news/news.py +++ b/hacker_news/news.py @@ -5,7 +5,9 @@ import time from json import JSONDecodeError -import openai +from openai import OpenAI + +client = OpenAI() import tiktoken from slugify import slugify @@ -188,17 +190,14 @@ def openai_complete(self, prompt, need_json): }}] kwargs['function_call'] = {"name": "render"} if config.openai_model.startswith('text-'): - resp = openai.Completion.create( - prompt=prompt, - **kwargs - ) + resp = client.completions.create(prompt=prompt, + **kwargs) answer = resp['choices'][0]['text'].strip() else: - resp = openai.ChatCompletion.create( - messages=[ - {'role': 'user', 'content': prompt}, - ], - **kwargs) + resp = client.chat.completions.create(messages=[ + {'role': 'user', 'content': prompt}, + ], + **kwargs) message = resp["choices"][0]["message"] if message.get('function_call'): json_str = message['function_call']['arguments']