Skip to content

Commit

Permalink
update ai util
Browse files Browse the repository at this point in the history
  • Loading branch information
parishwolfe committed Sep 23, 2024
1 parent 872aee3 commit 4ecb7a8
Showing 1 changed file with 24 additions and 8 deletions.
32 changes: 24 additions & 8 deletions util/ai_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,39 @@
from os import getenv

class ai_util:
def __init__(self, api_key=None):
self.api_key = api_key
if not self.api_key:
def __init__(
self,
api_key=None,
model="gpt-4o",
temperature=0.7,
max_response_len=None,
frequency_penalty=0
):
if not api_key:
self.api_key = getenv("OPENAI_API_KEY")
self.model = model
self.temperature = temperature
self.max_response_len = max_response_len
self.client = OpenAI()
self.frequency_penalty = frequency_penalty

def chat(self, messages: list):
completion = self.client.chat.completions.create(
model="gpt-4o",
messages=[
{"role": "user", "content": "write a haiku about ai"}
]
model=self.model,
messages=messages,
temperature=self.temperature,
max_tokens=self.max_response_len,
frequency_penalty=self.frequency_penalty
)
return completion.choices[0].message.content


# Example usage:
ai = ai_util()
response = ai.chat(["What is the meaning of life?"])
response = ai.chat(
[
{ "role": "system", "content": "You are a helpful assistant.",
"role": "user", "content": "Tell me about yourself" }
]
)
print(response)

0 comments on commit 4ecb7a8

Please sign in to comment.