File size: 1,168 Bytes
eae8f54
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
39
40
41
import json

from openai import OpenAI
from tenacity import retry, stop_after_attempt, wait_random_exponential

client = OpenAI()


# openai sent
@retry(wait=wait_random_exponential(multiplier=1, max=40), stop=stop_after_attempt(3))
def chat_completion_request(

    messages,

    tools=None,

    tool_choice=None,

    model="gpt-4o-mini",

    temperature=0.4,

    name="test",

    show=False,

):
    try:
        response = client.chat.completions.create(
            model=model,
            messages=messages,
            temperature=temperature,
            tools=tools,
            tool_choice=tool_choice,
            response_format={"type": "json_object"},
        )
        if show:
            print(f"input {name} = {messages}")
            print(f"output {name} = {response.choices[0].message.content}")
        return (
            json.loads(response.choices[0].message.content),
            response.usage.completion_tokens,
            response.usage.prompt_tokens,
        )
    except Exception as e:
        print("Unable to generate ChatCompletion response")
        print(f"Exception: {e}")
        return e