File size: 741 Bytes
38492d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import requests
import json
import openai
import configparser

# Load configuration
config = configparser.ConfigParser()
config.read('test.env')
API_KEY = config.get('API', 'OPEN_AI_KEY')
API_URL = config.get('API', 'OPEN_AI_URL')

# Set the OpenAI API key
openai.api_key = API_KEY

def generate(prompt):
    try:
        response = openai.ChatCompletion.create(
            model="gpt-4-turbo",
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": prompt}
            ],
            max_tokens=2000,
            temperature=0.9
        )
        return response['choices'][0]['message']['content']
    except Exception as e:
        return str(e)