wanglettes commited on
Commit
99bd06e
·
1 Parent(s): 2a13fc7

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -75
app.py CHANGED
@@ -1,82 +1,65 @@
1
- from typing import List, Tuple, Dict, Generator
2
- from langchain.llms import OpenAI
3
- import gradio as gr
4
- model_name = "gpt-3.5-turbo"
5
- LLM = OpenAI(model_name=model_name, temperature=0.1)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
 
7
- def create_history_messages(history: List[Tuple[str, str]]) -> List[dict]:
8
- history_messages = [{"role": "user", "content": m[0]} for m in history]
9
- history_messages.extend([{"role": "assistant", "content": m[1]} for m in history])
10
- return history_messages
11
 
12
- def create_formatted_history(history_messages: List[dict]) -> List[Tuple[str, str]]:
13
- formatted_history = []
14
- user_messages = []
15
- assistant_messages = []
16
 
17
- for message in history_messages:
18
- if message["role"] == "user":
19
- user_messages.append(message["content"])
20
- elif message["role"] == "assistant":
21
- assistant_messages.append(message["content"])
22
 
23
- if user_messages and assistant_messages:
24
- formatted_history.append(
25
- ("".join(user_messages), "".join(assistant_messages))
26
- )
27
- user_messages = []
28
- assistant_messages = []
 
 
 
 
 
29
 
30
- # append any remaining messages
31
- if user_messages:
32
- formatted_history.append(("".join(user_messages), None))
33
- elif assistant_messages:
34
- formatted_history.append((None, "".join(assistant_messages)))
 
 
 
35
 
36
- return formatted_history
37
 
38
- def chat(
39
- message: str, state: List[Dict[str, str]], client = LLM.client
40
- ) -> Generator[Tuple[List[Tuple[str, str]], List[Dict[str, str]]], None, None]:
41
- history_messages = state
42
- if history_messages == None:
43
- history_messages = []
44
- history_messages.append({"role": "system", "content": "A helpful assistant."})
45
-
46
- history_messages.append({"role": "user", "content": message})
47
- # We have no content for the assistant's response yet but we will update this:
48
- history_messages.append({"role": "assistant", "content": ""})
49
-
50
- response_message = ""
51
-
52
- chat_generator = client.create(
53
- messages=history_messages, stream=True, model=model_name
54
- )
55
-
56
- for chunk in chat_generator:
57
- if "choices" in chunk:
58
- for choice in chunk["choices"]:
59
- if "delta" in choice and "content" in choice["delta"]:
60
- new_token = choice["delta"]["content"]
61
- # Add the latest token:
62
- response_message += new_token
63
- # Update the assistant's response in our model:
64
- history_messages[-1]["content"] = response_message
65
-
66
- if "finish_reason" in choice and choice["finish_reason"] == "stop":
67
- break
68
- formatted_history = create_formatted_history(history_messages)
69
- yield formatted_history, history_messages
70
-
71
- chatbot = gr.Chatbot(label="Chat").style(color_map=("yellow", "purple"))
72
- iface = gr.Interface(
73
- fn=chat,
74
- inputs=[
75
- gr.Textbox(placeholder="Hello! How are you? etc.", label="Message"),
76
- "state",
77
- ],
78
- outputs=[chatbot, "state"],
79
- allow_flagging="never",
80
- )
81
-
82
- iface.queue().launch()
 
1
+ import requests
2
+ import pickle
3
+ # 用于存储API返回的上下文
4
+ class gpt:
5
+ def send_request(self, messages):
6
+ # 设置代理服务器的地址和端口
7
+ proxies = {
8
+ "http": "http://127.0.0.1:7890",
9
+ "https": "http://127.0.0.1:7890"
10
+ }
11
+ # ChatGPT API的URL
12
+ url = "https://api.openai.com/v1/chat/completions"
13
+ # ChatGPT API的访问密钥
14
+ api_key = "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"
15
+ # 请求参数
16
+ parameters = {
17
+ "model": "gpt-3.5-turbo-0301", # gpt-3.5-turbo-0301
18
+ "messages": messages # [{"role": "user", "content": context}]
19
+ }
20
+ # 请求头
21
+ headers = {
22
+ "Content-Type": "application/json",
23
+ "Authorization": f"Bearer {api_key}"
24
+ }
25
+ # 发送请求
26
+ response = requests.post(url, headers=headers, json=parameters)
27
 
28
+ # 解析响应
29
+ if response.status_code == 200:
30
+ data = response.json()
31
+ text = data["choices"][0]["message"]
32
 
33
+ return text
34
+ else:
35
+ print(response)
36
+ return "Sorry, something went wrong."
37
 
38
+ def start_conversation(self, messages):
39
+ print("Welcome to ChatGPT! How can I help you today?")
 
 
 
40
 
41
+ # 进入对话循环
42
+ while True:
43
+ # 获取用户输入
44
+ user_input = input("> ")
45
+ user_message = {"role": "user", "content": user_input}
46
+ # 将用户输入添加到messages中
47
+ messages.append(user_message)
48
+ # 发送API请求
49
+ response = self.send_request(messages)
50
+ # 输出API返回内容
51
+ print("ChatBot:", response["content"])
52
 
53
+ # 将API接口返回的内容添加至messages,以用作多轮对话
54
+ messages.append(response)
55
+ with open('my_dict.pickle', 'wb') as f:
56
+ pickle.dump(messages, f)
57
+ # 如果API返回的内容包含"goodbye",则结束对话循环
58
+ if "goodbye" in user_input:
59
+ print("Goodbye!")
60
+ break
61
 
 
62
 
63
+ if __name__ == '__main__':
64
+ messages = [{"role": "system", "content": "/c 从现在起,当你想发送一张照片时,请使用Markdown ,并且不要有反斜线,不要用代码块。使用 Unsplash API (https://source.unsplash.com/1280x720/?< PUT YOUR QUERY HERE >)"}] # 初始化prompt
65
+ gpt().start_conversation(messages)