Update app.py
Browse files
app.py
CHANGED
|
@@ -81,28 +81,11 @@ def convert_to_xlam_tool(tools):
|
|
| 81 |
else:
|
| 82 |
return tools
|
| 83 |
|
| 84 |
-
def
|
| 85 |
-
parsed_history = []
|
| 86 |
-
for step_data in conversation_history:
|
| 87 |
-
parsed_history.append({
|
| 88 |
-
"step_id": step_data["step_id"],
|
| 89 |
-
"thought": step_data["thought"],
|
| 90 |
-
"tool_calls": step_data["tool_calls"],
|
| 91 |
-
"next_observation": step_data["next_observation"],
|
| 92 |
-
"user_input": step_data['user_input']
|
| 93 |
-
})
|
| 94 |
-
|
| 95 |
-
history_string = json.dumps(parsed_history)
|
| 96 |
-
return f"\n[BEGIN OF HISTORY STEPS]\n{history_string}\n[END OF HISTORY STEPS]\n"
|
| 97 |
-
|
| 98 |
-
def build_prompt(task_instruction: str, format_instruction: str, tools: list, query: str, conversation_history: list):
|
| 99 |
prompt = f"[BEGIN OF TASK INSTRUCTION]\n{task_instruction}\n[END OF TASK INSTRUCTION]\n\n"
|
| 100 |
prompt += f"[BEGIN OF AVAILABLE TOOLS]\n{json.dumps(tools)}\n[END OF AVAILABLE TOOLS]\n\n"
|
| 101 |
prompt += f"[BEGIN OF FORMAT INSTRUCTION]\n{format_instruction}\n[END OF FORMAT INSTRUCTION]\n\n"
|
| 102 |
prompt += f"[BEGIN OF QUERY]\n{query}\n[END OF QUERY]\n\n"
|
| 103 |
-
|
| 104 |
-
if conversation_history and len(conversation_history) > 0:
|
| 105 |
-
prompt += build_conversation_history_prompt(conversation_history)
|
| 106 |
return prompt
|
| 107 |
|
| 108 |
def generate_response(tools_input, query):
|
|
@@ -112,7 +95,7 @@ def generate_response(tools_input, query):
|
|
| 112 |
return "Error: Invalid JSON format for tools input."
|
| 113 |
|
| 114 |
xlam_format_tools = convert_to_xlam_tool(tools)
|
| 115 |
-
content = build_prompt(task_instruction, format_instruction, xlam_format_tools, query
|
| 116 |
|
| 117 |
messages = [
|
| 118 |
{'role': 'user', 'content': content}
|
|
@@ -157,5 +140,41 @@ with gr.Blocks() as demo:
|
|
| 157 |
inputs=[tools_input, query_input],
|
| 158 |
)
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
if __name__ == "__main__":
|
| 161 |
demo.launch()
|
|
|
|
| 81 |
else:
|
| 82 |
return tools
|
| 83 |
|
| 84 |
+
def build_prompt(task_instruction: str, format_instruction: str, tools: list, query: str):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
prompt = f"[BEGIN OF TASK INSTRUCTION]\n{task_instruction}\n[END OF TASK INSTRUCTION]\n\n"
|
| 86 |
prompt += f"[BEGIN OF AVAILABLE TOOLS]\n{json.dumps(tools)}\n[END OF AVAILABLE TOOLS]\n\n"
|
| 87 |
prompt += f"[BEGIN OF FORMAT INSTRUCTION]\n{format_instruction}\n[END OF FORMAT INSTRUCTION]\n\n"
|
| 88 |
prompt += f"[BEGIN OF QUERY]\n{query}\n[END OF QUERY]\n\n"
|
|
|
|
|
|
|
|
|
|
| 89 |
return prompt
|
| 90 |
|
| 91 |
def generate_response(tools_input, query):
|
|
|
|
| 95 |
return "Error: Invalid JSON format for tools input."
|
| 96 |
|
| 97 |
xlam_format_tools = convert_to_xlam_tool(tools)
|
| 98 |
+
content = build_prompt(task_instruction, format_instruction, xlam_format_tools, query)
|
| 99 |
|
| 100 |
messages = [
|
| 101 |
{'role': 'user', 'content': content}
|
|
|
|
| 140 |
inputs=[tools_input, query_input],
|
| 141 |
)
|
| 142 |
|
| 143 |
+
if __name__ == "__main__":
|
| 144 |
+
demo.launch()
|
| 145 |
+
|
| 146 |
+
# Gradio interface
|
| 147 |
+
with gr.Blocks() as demo:
|
| 148 |
+
gr.Markdown(title)
|
| 149 |
+
gr.Markdown(description)
|
| 150 |
+
|
| 151 |
+
with gr.Row():
|
| 152 |
+
with gr.Column():
|
| 153 |
+
tools_input = gr.Code(
|
| 154 |
+
label="Available Tools (JSON format)",
|
| 155 |
+
lines=20,
|
| 156 |
+
value=example_tools,
|
| 157 |
+
language='json'
|
| 158 |
+
)
|
| 159 |
+
query_input = gr.Textbox(
|
| 160 |
+
label="User Query",
|
| 161 |
+
lines=2,
|
| 162 |
+
value=example_query
|
| 163 |
+
)
|
| 164 |
+
submit_button = gr.Button("Generate Response")
|
| 165 |
+
|
| 166 |
+
with gr.Column():
|
| 167 |
+
output = gr.Code(label="🎬 xLam :", lines=10, language="json")
|
| 168 |
+
|
| 169 |
+
submit_button.click(generate_response, inputs=[tools_input, query_input], outputs=output)
|
| 170 |
+
|
| 171 |
+
gr.Examples(
|
| 172 |
+
examples=[
|
| 173 |
+
[example_tools, "What's the weather like in San Francisco in celsius?"],
|
| 174 |
+
[example_tools, "Search for the latest news on artificial intelligence"],
|
| 175 |
+
],
|
| 176 |
+
inputs=[tools_input, query_input],
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
if __name__ == "__main__":
|
| 180 |
demo.launch()
|