Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .grit/grit.yaml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 9 additions & 10 deletions hacker_news/news.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import time
from json import JSONDecodeError

import openai
from openai import OpenAI

client = OpenAI()
import tiktoken
from slugify import slugify

Expand Down Expand Up @@ -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']
Expand Down