File size: 2,058 Bytes
06b1340
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31fbac4
 
 
 
 
 
 
 
 
 
 
 
cc25147
06b1340
 
 
 
 
 
 
 
 
 
 
 
 
cc25147
 
06b1340
 
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
50
51
52
53
54
55
56
57
58
59
import openai
import gradio as gr
import json

openai.api_key = "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"

def save_conversation():
    with open('conversation.json', 'w') as f:
        json.dump(messages, f)

def load_conversation():
    try:
        with open('conversation.json', 'r') as f:
            return json.load(f)
    except FileNotFoundError:
        return []
messages = load_conversation()

if not messages:
    messages.append({"role": "system", "content": "You are a super computer coding assistant, capable of solving any programming problem. You are equipped with the knowledge of various programming languages and tools, and can assist beginners in quickly creating incredibly powerful code."})


def CustomChatGPT(user_input):
    messages.append({"role": "user", "content": user_input})

    # Ensure the conversation fits within the model's maximum token limit
    conversation = messages[-4096:]

  try:
    # Code that might raise an exception
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=conversation,
        max_tokens=1000,
        temperature=0.7)
except openai.error.RequestError as e:
    # Code to run if the exception is raised
    print(f"Received error from OpenAI: {e}")
    return "I'm sorry, but I'm unable to generate a response at this time."

except openai.error.RequestError as e:
        print(f"Received error from OpenAI: {e}")
        return "I'm sorry, but I'm unable to generate a response at this time."

    ChatGPT_reply = response["choices"][0]["message"]["content"]
    messages.append({"role": "assistant", "content": ChatGPT_reply})
    
    save_conversation()

    return ChatGPT_reply

interface = gr.Interface(fn=CustomChatGPT, 
                         inputs="textbox", 
                         outputs="textbox", 
                         title="Coding Assistant AL", 
                         description="Coding Quantum Assistant, Trained on all relevant languages as of June 2, 2023. Developed by A. Leschik.")

interface.launch()