Omnibus commited on
Commit
6d9872e
·
1 Parent(s): 4f737eb

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +189 -0
app.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ #import subprocess
3
+ from huggingface_hub import InferenceClient
4
+ import gradio as gr
5
+ client = InferenceClient(
6
+ "mistralai/Mixtral-8x7B-Instruct-v0.1"
7
+ )
8
+
9
+ def parse_action(string: str):
10
+ assert string.startswith("action:")
11
+ idx = string.find("action_input=")
12
+ if idx == -1:
13
+ return string[8:], None
14
+ return string[8 : idx - 1], string[idx + 13 :].strip("'").strip('"')
15
+
16
+
17
+ from prompts import (
18
+ ACTION_PROMPT,
19
+ COMPRESS_HISTORY_PROMPT,
20
+ LOG_PROMPT,
21
+ LOG_RESPONSE,
22
+ PREFIX,
23
+ TASK_PROMPT,
24
+ )
25
+
26
+ VERBOSE = True
27
+ MAX_HISTORY = 100
28
+ #MODEL = "gpt-3.5-turbo" # "gpt-4"
29
+
30
+
31
+ def format_prompt(message, history):
32
+ prompt = "<s>"
33
+ for user_prompt, bot_response in history:
34
+ prompt += f"[INST] {user_prompt} [/INST]"
35
+ prompt += f" {bot_response}</s> "
36
+ prompt += f"[INST] {message} [/INST]"
37
+ return prompt
38
+
39
+
40
+
41
+ def run_gpt(
42
+ prompt_template,
43
+ stop_tokens,
44
+ max_tokens,
45
+ module_summary,
46
+ purpose,
47
+ **prompt_kwargs,
48
+ ):
49
+
50
+ generate_kwargs = dict(
51
+ temperature=0.9,
52
+ max_new_tokens=1048,
53
+ top_p=0.95,
54
+ repetition_penalty=1.0,
55
+ do_sample=True,
56
+ seed=1,
57
+ )
58
+
59
+
60
+ content = PREFIX.format(
61
+ purpose=purpose,
62
+ ) + prompt_template.format(**prompt_kwargs)
63
+ if VERBOSE:
64
+ print(LOG_PROMPT.format(content))
65
+
66
+
67
+ #formatted_prompt = format_prompt(f"{system_prompt}, {prompt}", history)
68
+ #formatted_prompt = format_prompt(f'{content}', history)
69
+
70
+ stream = client.text_generation(content, **generate_kwargs, stream=True, details=True, return_full_text=False)
71
+ resp = ""
72
+ for response in stream:
73
+ resp += response.token.text
74
+ #yield resp
75
+
76
+ if VERBOSE:
77
+ print(LOG_RESPONSE.format(resp))
78
+ return resp
79
+
80
+
81
+ def compress_history(purpose, task, history):
82
+ resp = run_gpt(
83
+ COMPRESS_HISTORY_PROMPT,
84
+ stop_tokens=["observation:", "task:", "action:", "thought:"],
85
+ max_tokens=512,
86
+ purpose=purpose,
87
+ task=task,
88
+ history=history,
89
+ )
90
+ history = "observation: {}\n".format(resp)
91
+ return history
92
+
93
+
94
+ def call_main(purpose, task, history, action_input):
95
+ resp = run_gpt(
96
+ ACTION_PROMPT,
97
+ stop_tokens=["observation:", "task:"],
98
+ max_tokens=256,
99
+ purpose=purpose,
100
+ task=task,
101
+ history=history,
102
+ )
103
+ lines = resp.strip().strip("\n").split("\n")
104
+ for line in lines:
105
+ if line == "":
106
+ continue
107
+ if line.startswith("thought: "):
108
+ history += "{}\n".format(line)
109
+ elif line.startswith("action: "):
110
+ action_name, action_input = parse_action(line)
111
+ history += "{}\n".format(line)
112
+ return action_name, action_input, history, task
113
+ else:
114
+ assert False, "unknown action: {}".format(line)
115
+ return "MAIN", None, history, task
116
+
117
+
118
+ def call_set_task(purpose, task, history, action_input):
119
+ task = run_gpt(
120
+ TASK_PROMPT,
121
+ stop_tokens=[],
122
+ max_tokens=64,
123
+ purpose=purpose,
124
+ task=task,
125
+ history=history,
126
+ ).strip("\n")
127
+ history += "observation: task has been updated to: {}\n".format(task)
128
+ return "MAIN", None, history, task
129
+
130
+
131
+ NAME_TO_FUNC = {
132
+ "MAIN": call_main,
133
+ "UPDATE-TASK": call_set_task,
134
+ "SEARCH": call_search,
135
+
136
+ }
137
+
138
+
139
+ def run_action(purpose, task, history, action_name, action_input):
140
+ if action_name == "COMPLETE":
141
+ exit(0)
142
+
143
+ # compress the history when it is long
144
+ if len(history.split("\n")) > MAX_HISTORY:
145
+ if VERBOSE:
146
+ print("COMPRESSING HISTORY")
147
+ history = compress_history(purpose, task, history)
148
+
149
+ assert action_name in NAME_TO_FUNC
150
+
151
+ print("RUN: ", action_name, action_input)
152
+ return NAME_TO_FUNC[action_name](purpose, task, history, action_input)
153
+
154
+
155
+ def run(purpose,history):
156
+ task=None
157
+ history = ""
158
+ #if not history:
159
+ # history = []
160
+ action_name = "UPDATE-TASK" if task is None else "MAIN"
161
+ action_input = None
162
+ while True:
163
+ print("")
164
+ print("")
165
+ print("---")
166
+ print("purpose:", purpose)
167
+ print("task:", task)
168
+ print("---")
169
+ print(history)
170
+ print("---")
171
+
172
+ action_name, action_input, history, task = run_action(
173
+ purpose,
174
+ task,
175
+ history,
176
+ action_name,
177
+ action_input,
178
+ )
179
+
180
+
181
+ gr.ChatInterface(
182
+ fn=run,
183
+ chatbot=gr.Chatbot(show_label=False, show_share_button=False, show_copy_button=True, likeable=True, layout="panel"),
184
+ title="Mixtral 46.7B\nMicro-Agent\nInternet Search <br> development test",
185
+ examples=examples,
186
+ concurrency_limit=20,
187
+ ).launch(show_api=False)
188
+
189
+