forked from hassancs91/AI-Agents-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai_module.py
32 lines (22 loc) · 840 Bytes
/
openai_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from openai import OpenAI
import os
from dotenv import load_dotenv
# Load environment variables
load_dotenv()
# Create an instance of the OpenAI class
openai_client = OpenAI(api_key=os.getenv("OPENAI_API_KEY"))
def generate_text_basic(prompt: str, model = "gpt-3.5-turbo", system_prompt: str = "You are a helpfull ai assistant"):
response = openai_client.chat.completions.create(
model=model,
messages=[
{"role": "system", "content": system_prompt},
{"role": "user", "content": prompt}
]
)
return response.choices[0].message.content
def generate_text_with_conversation(messages,model = "gpt-3.5-turbo"):
response = openai_client.chat.completions.create(
model=model,
messages=messages
)
return response.choices[0].message.content