-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.py
88 lines (73 loc) · 2.65 KB
/
app.py
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 gradio as gr
from langchain_core.messages import HumanMessage, AIMessage
from agent import graph
def parse_function_call(output: str):
# Example: <function=run_shell_command {"command": "whoami"}>
func_name = None
payload = {}
if output.startswith("<function="):
end_func = output.find(" ")
func_name = output[10:end_func]
json_str = output[end_func:].trip(" {}>")
import json
try:
payload = json.loads(json_str)
except Exception:
pass
return func_name, payload
async def predict(message, history, state):
config = state
history_langchain_format = []
for msg in history:
if msg['role'] == "user":
history_langchain_format.append(HumanMessage(content=msg['content']))
elif msg['role'] == "assistant":
history_langchain_format.append(AIMessage(content=msg['content']))
history_langchain_format.append(HumanMessage(content=message))
gpt_response = await graph.ainvoke({
"messages": history_langchain_format
}, config=config)
output = gpt_response['messages'][-1].content
if output.startswith("<function="):
func_name, payload = parse_function_call(output)
if func_name == "run_shell_command":
cmd = payload.get("command")
# e.g., execute cmd
return f"<Executing '{cmd}' here>"
return output
def update_key(key,state):
state["configurable"] = {
"api_key": key
}
gr.Info("API Key Configured...")
return state
with gr.Blocks(theme=gr.themes.Soft()) as chat:
state = gr.State()
state.value = { }
gr.Markdown("""
# PCBot - Chat with Your Laptop
Interact with your OS using natural language. Ask questions about files, processes, and system information.
""")
with gr.Tab("Chat"):
chatbot = gr.ChatInterface(
fn=predict,
type="messages",
additional_inputs=[state],
concurrency_limit=10,
title="Chat Interface"
)
with gr.Tab("Settings"):
with gr.Group():
with gr.Row():
key = gr.Textbox(
lines=1,
label="GROQ API Key",
placeholder="Enter your API key here...",
type="password"
)
button = gr.Button("Save Key", variant="primary")
gr.Markdown("*Your API key is stored temporarily and will be cleared when you close the browser.*")
button.click(update_key, [key, state], [state])
gr.Markdown("---\n*Powered by GROQ LLM*")
if __name__ == "__main__":
chat.launch()