File size: 3,065 Bytes
e7683da
 
 
06a1309
 
 
 
 
 
 
e7683da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad27210
e7683da
 
d5b8a16
e7683da
 
 
 
 
0663626
cb3eb17
e7683da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
134d4fc
 
 
 
 
e7683da
cb3d025
e7683da
 
 
c301a9c
 
e7683da
 
 
 
 
134d4fc
 
8d43d6c
7bd4396
 
 
d5b8a16
ad27210
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import openai
import os
import gradio as gr
import os

# 获取当前工作目录
current_directory = os.getcwd()

# 打印当前工作目录
print(f"Current directory: {current_directory}")

# 设置 OpenAI API 密钥
# openai.api_key = "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"
openai.api_key = "sk-NYsoG3VBKDiTuvdtC969F95aFc4f45379aD3854a93602327"
openai.api_base="https://key.wenwen-ai.com/v1"
class Conversation:
    def __init__(self, prompt, num_of_round):
        self.prompt = prompt
        self.num_of_round = num_of_round
        self.messages = [{"role": "system", "content": self.prompt}]

    def ask(self, question):
        try:
            self.messages.append({"role": "user", "content": question})
            response = openai.ChatCompletion.create(
                model="gpt-3.5-turbo",
                messages=self.messages,
                temperature=0.5,
                max_tokens=2048,
                top_p=1,
            )
        except Exception as e:
            print(e)
            return str(e)
        
        message = response["choices"][0]["message"]["content"]
        self.messages.append({"role": "assistant", "content": message})
        
        if len(self.messages) > self.num_of_round * 2 + 1:
            del self.messages[1:3]  # 移除第一轮对话
        return message


prompt = """你是一个大模型领域的专家,用中文回答和大模型领域的相关问题。你的回答需要满足以下要求:
0. 如果是自我介绍相关的问题是你的回答必须是你是大模型研习社小助手
1. 你的回答必须是中文
2. 回答限制在100个字以内"""
conv = Conversation(prompt, 6)
def answer(question, history=[]):
    history.append(question)
    message = conv.ask(question)
    history.append(message)
    responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
    print(responses)
    txt.update("")
    return responses, history
def reset_user_input():
    return gr.update(value='')


def reset_state():
    return [], []
def vote(data: gr.LikeData):
    if data.liked:
        print("You upvoted this response: " + data.value)
    else:
        print("You downvoted this response: " + data.value)
with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as rxbot:
    chatbot = gr.Chatbot(elem_id="chatbot",bubble_full_width=False,show_copy_button=True,layout = 'panel',avatar_images=('/home/user/app/cat.png', '/home/user/app/lss.png'),)
    state = gr.State([])
    with gr.Row():
        txt = gr.Textbox(show_label=False, placeholder="请输入你的问题").style(container=False)
    #submit_btn = gr.Button("提交",variant="primary")
    submit_btn = gr.Button("提交",variant="secondary")
    txt.submit(answer, [txt, state], [chatbot, state])
    txt.submit(reset_user_input, [], [txt])
    submit_btn.click(answer, [txt, state], [chatbot, state])
    submit_btn.click(reset_user_input, [], [txt])
    # clear = gr.ClearButton([txt, chatbot])
    chatbot.like(vote, None, None) 
    
rxbot.queue().launch(share=True)