-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenai_client.py
More file actions
38 lines (32 loc) · 1.05 KB
/
openai_client.py
File metadata and controls
38 lines (32 loc) · 1.05 KB
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
31
32
33
34
35
36
37
38
import json
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
# Initialize OpenAI client for chat-based function calling
client = OpenAI()
def get_chat_function_call(
instructions,
user_input,
functions,
function_call_name,
model="gpt-5",
temperature=None,
):
"""Call OpenAI chat completion with function calling and return the parsed JSON arguments."""
# Ensure functions is a list of function specifications
if not isinstance(functions, list):
functions = [functions]
request_kwargs = {
"model": model,
"messages": [
{"role": "system", "content": instructions},
{"role": "user", "content": user_input},
],
"functions": functions,
"function_call": {"name": function_call_name},
}
if temperature is not None:
request_kwargs["temperature"] = temperature
response = client.chat.completions.create(**request_kwargs)
arguments = response.choices[0].message.function_call.arguments
return json.loads(arguments)