kanli commited on
Commit
e7683da
·
1 Parent(s): a519d7c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +65 -105
app.py CHANGED
@@ -1,113 +1,73 @@
1
- # import openai
2
- # import os
3
- # import gradio as gr
4
-
5
- # # 设置 OpenAI API 密钥
6
- # # openai.api_key = "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"
7
- # openai.api_key = "sk-NYsoG3VBKDiTuvdtC969F95aFc4f45379aD3854a93602327"
8
- # openai.api_base="https://key.wenwen-ai.com/v1"
9
- # class Conversation:
10
- # def __init__(self, prompt, num_of_round):
11
- # self.prompt = prompt
12
- # self.num_of_round = num_of_round
13
- # self.messages = [{"role": "system", "content": self.prompt}]
14
-
15
- # def ask(self, question):
16
- # try:
17
- # self.messages.append({"role": "user", "content": question})
18
- # response = openai.ChatCompletion.create(
19
- # model="gpt-3.5-turbo",
20
- # messages=self.messages,
21
- # temperature=0.5,
22
- # max_tokens=2048,
23
- # top_p=1,
24
- # )
25
- # except Exception as e:
26
- # print(e)
27
- # return str(e)
28
 
29
- # message = response["choices"][0]["message"]["content"]
30
- # self.messages.append({"role": "assistant", "content": message})
31
 
32
- # if len(self.messages) > self.num_of_round * 2 + 1:
33
- # del self.messages[1:3] # 移除第一轮对话
34
- # return message
35
-
36
-
37
- # prompt = """你是一个大数据和AI领域的专家,用中文回答大数据和AI的相关问题。你的回答需要满足以下要求:
38
- # 1. 你的回答必须是中文
39
- # 2. 回答限制在100个字以内"""
40
- # conv = Conversation(prompt, 6)
41
- # def answer(question, history=[]):
42
- # history.append(question)
43
- # message = conv.ask(question)
44
- # history.append(message)
45
- # responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
46
- # print(responses)
47
- # txt.update("")
48
- # return responses, history
49
- # def reset_user_input():
50
- # return gr.update(value='')
51
-
52
-
53
- # def reset_state():
54
- # return [], []
55
-
56
- # with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as rxbot:
57
- # chatbot = gr.Chatbot(elem_id="chatbot")
58
- # state = gr.State([])
59
- # with gr.Row():
60
- # txt = gr.Textbox(show_label=False, placeholder="请输入你的问题").style(container=False)
61
- # submit_btn = gr.Button("提交")
62
- # txt.submit(answer, [txt, state], [chatbot, state])
63
- # txt.submit(reset_user_input, [], [txt])
64
- # submit_btn.click(answer, [txt, state], [chatbot, state])
65
- # submit_btn.click(reset_user_input, [], [txt])
66
- # # clear = gr.ClearButton([txt, chatbot])
67
 
68
- # rxbot.launch(share=True)
69
-
70
-
71
-
72
-
73
-
74
- import os
75
- import openai
76
- # 设置网络代理
77
- # os.environ["http_proxy"] = "http://127.0.0.1:1080"
78
- # os.environ["https_proxy"] = "http://127.0.0.1:1080"
79
- # os.environ["OPENAI_API_KEY"] = '你的api key'
80
- os.environ["SERPAPI_API_KEY"] = '4f9b45ca814fa43f2b4268fcd07f55860760560b0ff7a5ff66f49e52d7d88ec2'
81
- os.environ["OPENAI_API_KEY"] = "sk-NYsoG3VBKDiTuvdtC969F95aFc4f45379aD3854a93602327"
82
- os.environ["OPENAI_BASE"] = "https://key.wenwen-ai.com/v1"
83
- # openai.api_key = "sk-NYsoG3VBKDiTuvdtC969F95aFc4f45379aD3854a93602327"
84
- # openai.api_base="https://key.wenwen-ai.com/v1"
85
- from langchain.agents import load_tools
86
- from langchain.agents import initialize_agent
87
- from langchain.llms import OpenAI
88
- from langchain.agents import AgentType
89
-
90
- from langchain.agents import tool
91
- from datetime import date
92
 
93
- @tool
94
- def time(text: str) -> str:
95
- """Returns todays date, use this for any \
96
- questions related to knowing todays date. \
97
- The input should always be an empty string, \
98
- and this function will always return todays \
99
- date - any date mathmatics should occur \
100
- outside this function."""
101
- return str(date.today())
102
 
103
- # 加载 OpenAI 模型
104
- llm = OpenAI(temperature=0,max_tokens=2048)
105
 
106
- # 加载 serpapi 工具
107
- tools = load_tools(["serpapi"])
108
 
109
- # 工具加载后都需要初始化,verbose 参数为 True,会打印全部的执行详情
110
- agent = initialize_agent(tools + [time], llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION, verbose=True)
111
 
112
- # 运行 agent
113
- agent.run("今天是几号? 今天微博的热搜话题有哪些?")
 
1
+ import openai
2
+ import os
3
+ import gradio as gr
4
+
5
+ # 设置 OpenAI API 密钥
6
+ # openai.api_key = "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"
7
+ openai.api_key = "sk-NYsoG3VBKDiTuvdtC969F95aFc4f45379aD3854a93602327"
8
+ openai.api_base="https://key.wenwen-ai.com/v1"
9
+ class Conversation:
10
+ def __init__(self, prompt, num_of_round):
11
+ self.prompt = prompt
12
+ self.num_of_round = num_of_round
13
+ self.messages = [{"role": "system", "content": self.prompt}]
14
+
15
+ def ask(self, question):
16
+ try:
17
+ self.messages.append({"role": "user", "content": question})
18
+ response = openai.ChatCompletion.create(
19
+ model="gpt-3.5-turbo",
20
+ messages=self.messages,
21
+ temperature=0.5,
22
+ max_tokens=2048,
23
+ top_p=1,
24
+ )
25
+ except Exception as e:
26
+ print(e)
27
+ return str(e)
28
 
29
+ message = response["choices"][0]["message"]["content"]
30
+ self.messages.append({"role": "assistant", "content": message})
31
 
32
+ if len(self.messages) > self.num_of_round * 2 + 1:
33
+ del self.messages[1:3] # 移除第一轮对话
34
+ return message
35
+
36
+
37
+ prompt = """你是一个大数据和AI领域的专家,用中文回答大数据和AI的相关问题。你的回答需要满足以下要求:
38
+ 1. 你的回答必须是中文
39
+ 2. 回答限制在100个字以内"""
40
+ conv = Conversation(prompt, 6)
41
+ def answer(question, history=[]):
42
+ history.append(question)
43
+ message = conv.ask(question)
44
+ history.append(message)
45
+ responses = [(u,b) for u,b in zip(history[::2], history[1::2])]
46
+ print(responses)
47
+ txt.update("")
48
+ return responses, history
49
+ def reset_user_input():
50
+ return gr.update(value='')
51
+
52
+
53
+ def reset_state():
54
+ return [], []
55
+
56
+ with gr.Blocks(css="#chatbot{height:300px} .overflow-y-auto{height:500px}") as rxbot:
57
+ chatbot = gr.Chatbot(elem_id="chatbot")
58
+ state = gr.State([])
59
+ with gr.Row():
60
+ txt = gr.Textbox(show_label=False, placeholder="请输入你的问题").style(container=False)
61
+ submit_btn = gr.Button("提交")
62
+ txt.submit(answer, [txt, state], [chatbot, state])
63
+ txt.submit(reset_user_input, [], [txt])
64
+ submit_btn.click(answer, [txt, state], [chatbot, state])
65
+ submit_btn.click(reset_user_input, [], [txt])
66
+ # clear = gr.ClearButton([txt, chatbot])
67
 
68
+ rxbot.launch(share=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
 
 
 
 
 
 
 
 
 
70
 
 
 
71
 
 
 
72
 
 
 
73