File size: 1,118 Bytes
a8467fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
import os
import openai
import json
from pprint import pprint
from tenacity import retry, wait_random_exponential, stop_after_attempt

openai.api_key = os.environ.get("OPENAI_API_KEY")
MODEL = 'gpt-4'

'''
basic underlying method to get completions. retries built in
'''
@retry(wait=wait_random_exponential(multiplier=2, max=10), stop=stop_after_attempt(3))
def get_completion(messages, temperature=1):

    try:
        chat_completion = openai.ChatCompletion.create(
            model=MODEL,
            temperature=temperature,
            messages=messages
        )

        return chat_completion.choices[0].message.content

    except Exception as e:
        print("Unable to generate ChatCompletion response")
        print(f"Exception: {e}")
        return e

'''
basic text completion
'''
def answer(system_message, user_message, temperature=1):

    messages = [{
      "role": "system",
      "content": system_message
    },{
      "role": "user",
      "content": user_message
    }]

    completion = get_completion(
      messages=messages,
      temperature = temperature
    )

    return completion