File size: 1,670 Bytes
48e1709
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import openai
import gradio as gr
import os
from functools import reduce

openai.api_key = os.getenv("OPENAI_API_KEY")

def manage_conversation_history(messages, max_tokens=4096):
    total_tokens = sum([len(msg["content"]) for msg in messages])
    while total_tokens > max_tokens:
        # Remove the oldest user and assistant messages
        messages.pop(1)
        messages.pop(1)
        total_tokens = sum([len(msg["content"]) for msg in messages])
    return messages

messages = [{"role": "system", "content": "You are a Web3 and cryptocurrency expert that explains Web3, cryptocurrency, blockchain, and financial terminology in terms so simple even a five year old could understand it. If you ever use technical words, terms, or phrases, you create relatable analogies to simplify them and make them easier to understand. In fact, you always open with an analogy when possible."}]

def CustomChatGPT(user_input):
    messages.append({"role": "user", "content": user_input})
    managed_messages = manage_conversation_history(messages)
    response = openai.ChatCompletion.create(
        model="gpt-3.5-turbo",
        messages=managed_messages
    )
    ChatGPT_reply = response["choices"][0]["message"]["content"]
    messages.append({"role": "assistant", "content": ChatGPT_reply})
    return ChatGPT_reply

description = "Check for ChatGPT outages <a href=https://status.openai.com/>here.</a>"  

demo = gr.Interface(
    fn=CustomChatGPT,
    inputs=gr.Textbox(label="Ask a question:", placeholder="E.g. What are gas fees? ...can you simplify that with an analogy?"),
    outputs=gr.Textbox(label="Answer:"),
    title=""
    )

demo.launch(inline=True)