Skip to content

Commit

Permalink
switch to new openai client
Browse files Browse the repository at this point in the history
  • Loading branch information
samshapley committed Nov 29, 2023
1 parent 3e02a8b commit d5173eb
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 14 deletions.
1 change: 0 additions & 1 deletion agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import atexit
import json
import os
import openai

# create logs directory if it doesn't exist
if not os.path.exists('logs'):
Expand Down
20 changes: 10 additions & 10 deletions ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
default_presence_penalty = config['default_presence_penalty']

class AI:
def __init__(self, module_name, model_config=None, openai=openai):
self.openai = openai
def __init__(self, module_name, model_config=None, openai_client=None):
self.openai = openai_client or openai
self.model_config = model_config
self.model = model_config.get('model', default_model) if model_config else default_model
self.temperature = model_config.get('temperature', default_temperature) if model_config else default_temperature
Expand All @@ -36,7 +36,7 @@ def generate_response(self, prompt):
token_count = 0

try:
response = self.openai.ChatCompletion.create(
response = self.openai.chat.completions.create(
model=self.model,
stream=True,
messages=self.messages,
Expand All @@ -50,14 +50,13 @@ def generate_response(self, prompt):
chat = []
token_count = 0
for chunk in response:
delta = chunk["choices"][0]["delta"]
msg = delta.get("content", "")
print(msg, end="")
chat.append(msg)
token_count += len(msg.split()) # estimate token usage

print()
msg = chunk.choices[0].delta.content
if msg is not None:
print(msg, end="")
chat.append(msg)
token_count += len(msg.split()) # estimate token usage

print(len(chat))
response_text = "".join(chat)

llm_end_time_ms = round(datetime.datetime.now().timestamp() * 1000) # logged in milliseconds
Expand All @@ -66,6 +65,7 @@ def generate_response(self, prompt):
token_usage = {"total_tokens": token_count}

except Exception as e:
print("Error: ", e)
llm_end_time_ms = round(datetime.datetime.now().timestamp() * 1000) # logged in milliseconds
status_code="error"
status_message=str(e)
Expand Down
4 changes: 2 additions & 2 deletions config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pipeline: 'engineering_pipeline'
### OpenAI Default Configs ###
### All calls to OpenAI will use these configs unless otherwise specified in pipeline ###

default_model: 'gpt-4' # Recommended to set to a cheaper model for testing.
default_temperature: 0.8
default_model: 'gpt-4-0613' # Recommended to set to a cheaper model for testing.
default_temperature: 0.7
default_max_tokens: Null
default_top_p: 1
default_frequency_penalty: 0
Expand Down
2 changes: 1 addition & 1 deletion helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def authenticate():
def is_valid_api_key(api_key):
try:
openai.api_key = api_key
openai.Model.list() # Make a simple API request
openai.models.list() # Make a simple API request
return True
except Exception:
print("\033[91mInvalid API key.\033[00m")
Expand Down

0 comments on commit d5173eb

Please sign in to comment.