jifangw
2024-10-11 f60d836ec6f134e45883b6dcdc3924315f923628
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
42
43
44
45
46
47
48
import os
import openai
from openai import OpenAI
 
 
api_key = "xxxxxxxxxxxxxx"
organization = "xxxxxxxxxxxxxx"
 
def GPTCall(prompt):
    counter = 0
    result = "api调用失败"
    while counter < 3:
        try:
            openai.api_key = api_key
            openai.organization = organization
            client = OpenAI(api_key = api_key, organization = organization)
            completion = client.chat.completions.create(
                model = "gpt-4o",
                # model = "gpt-3.5-turbo",
                # model="gpt-4-1106-preview",
                messages=[
                    {"role": "user", "content": prompt},
                ]
            )
            result = completion.choices[0].message.content
            print(f"%%%%%%%%%%%%%%%%%%%%%%%%\n{result}\n%%%%%%%%%%%%%%%%%%%%%%%")
            break
        
        except Exception as e:
            print(e)
            counter += 1
            
    return result
 
 
def GPTTTS(text, role):
    
    openai.api_key = api_key
    openai.organization = organization
    client = OpenAI(api_key = api_key, organization = organization)
    response = client.audio.speech.create(
        model = "tts-1",
        voice = role,
        input = text,
        response_format = "mp3"
    )
    
    return response