File size: 1,086 Bytes
2c80d98
 
 
c1462f3
2c80d98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3f53b5c
2c80d98
 
 
 
 
 
 
ab562bb
2c80d98
c5c8c48
 
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
import openai
import os 
import dotenv
from helper import open_file

## Read the environment variables
dotenv.load_dotenv('.env')
openai.api_key = os.getenv('OPENAI_API_KEY')
llm_model = os.getenv('LLM_MODEL')


def call_gpt(prompt):
    max_retry = 5
    retry = 0
    prompt = prompt.encode(encoding='ASCII',errors='ignore').decode()
    while True:
        try:
            response = openai.ChatCompletion.create(
                            model=llm_model,
                            temperature=0.9,
                            messages=[
                                {"role": "user", "content": prompt}
                            ]
            )
            
            text = response.choices[0].message.content
            return text
        except Exception as oops:
            retry += 1
            if retry >= max_retry:
                return "GPT3 error: %s" % oops
            print('Error communicating with OpenAI:', oops)
            sleep(1)

def call_llm():
    prompt = open_file('prompt_response.txt')
    response = call_gpt(prompt)
    return response