Aakash jammula commited on
Commit
b216352
·
1 Parent(s): fea8823

Deploy agent

Browse files
Files changed (3) hide show
  1. Dockerfile +11 -0
  2. app.py +119 -0
  3. requirements.txt +8 -0
Dockerfile ADDED
@@ -0,0 +1,11 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.9-slim
2
+ RUN apt-get update && apt-get install -y curl \
3
+ && curl -sSL https://ollama.com/install.sh | bash \
4
+ && ollama pull gemma3:4b
5
+ WORKDIR /app
6
+ COPY requirements.txt .
7
+ RUN pip install --no-cache-dir -r requirements.txt
8
+ COPY . .
9
+ EXPOSE 7860
10
+ CMD ollama run gemma3:4b --api --port 11434 & \
11
+ uvicorn app:app --host 0.0.0.0 --port 7860
app.py ADDED
@@ -0,0 +1,119 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from langchain_core.messages import HumanMessage, SystemMessage
2
+ from langgraph.graph import MessagesState
3
+ from langgraph.graph import StateGraph, START, END
4
+ from langchain_tavily import TavilySearch
5
+ from langchain_experimental.utilities import PythonREPL
6
+ from langgraph.graph import MessagesState
7
+ from langchain_core.tools import Tool
8
+ from langgraph.prebuilt import tools_condition, ToolNode
9
+ from fastapi import FastAPI
10
+ from fastapi.responses import JSONResponse
11
+ from langchain_ollama import ChatOllama
12
+ from pydantic import BaseModel
13
+
14
+ from langgraph.checkpoint.memory import MemorySaver
15
+
16
+
17
+ llm = ChatOllama(model="gemma3:4b", temperature=0.5)
18
+ app = FastAPI()
19
+
20
+ # Tavily Web Search Tool
21
+ search_tool = TavilySearch()
22
+
23
+ # Calculator Tool (simple math)
24
+ calculator_tool = Tool.from_function(
25
+ name="Calculator",
26
+ func=lambda x: str(eval(x)),
27
+ description="Performs basic arithmetic operations like add, subtract, multiply, divide."
28
+ )
29
+
30
+ # Python REPL Tool (advanced logic/math)
31
+ python_repl = PythonREPL()
32
+ python_tool = Tool.from_function(
33
+ name="PythonREPL",
34
+ func=python_repl.run,
35
+ description="Executes advanced Python code like loops, conditionals, etc."
36
+ )
37
+
38
+ # Combine all tools
39
+ tools = [search_tool, calculator_tool, python_tool]
40
+ llm_with_tools = llm.bind_tools(tools)
41
+
42
+
43
+ class State(MessagesState):
44
+ prompt_enhanced: str
45
+
46
+ def prompt_enhancer(state: State) -> State:
47
+ messages = state.get("messages", [])
48
+ last = messages[-1]
49
+ enhancer_system = SystemMessage(content=(
50
+ "You are PromptEnhancer (aka Jarvis), a smart, friendly assistant helping user. "
51
+ "Your job is to turn the user's raw request into a minimal JSON object with two fields:\n"
52
+ " • tools: a list of tool names to invoke\n"
53
+ " • action: a concise description of what to do\n\n"
54
+ "Available tools:\n"
55
+ " - search_tool = TavilySearch()\n"
56
+ " - calculator = Tool.from_function(name='Calculator', func=lambda x: str(eval(x)), description='Basic arithmetic')\n"
57
+ " - python_repl = PythonREPL()\n"
58
+ " - python_tool = Tool.from_function(name='PythonREPL', func=python_repl.run, description='Run Python code')\n\n"
59
+ "use multiple tools if needed, and make sure to include the action field. "
60
+ "if time is a factor, use the search tool to find the answer. "
61
+ "Output the raw JSON object exactly as-is, without any markdown or code fences, and no extra text."
62
+ ))
63
+ enhanced = llm.invoke([enhancer_system] + [last])
64
+ state["prompt_enhanced"] = enhanced.content
65
+ return state
66
+
67
+ def assistant(state: State) -> State:
68
+ messages = state.get("messages", [])
69
+ thinking = state.get("prompt_enhanced", None)
70
+ sys_msg = SystemMessage(content=(
71
+ "You are Jarvis, a smart and friendly personal AI assistant helping user. "
72
+ "Your primary functions are helping Aakash with math, coding, and general questions. "
73
+ "For simple arithmetic, please use the Calculator Tool. "
74
+ "For tasks involving complex logic, loops, or functions, utilize the Python Tool. "
75
+ "To find answers about current events or real-world topics or news or weather or learning a new topic, use the Search Tool. "
76
+ "Always provide a brief explanation for your approach. "
77
+ "here is the JSON object you received from PromptEnhancer:\n\n"
78
+ f"{thinking}\n\n"
79
+ "this json object contains two fields: tools and action. "
80
+ "The tools field is a list of tool names to invoke, and the action field is a concise description of what to do. "
81
+ "Strive to be concise, accurate, and polite in all your responses. "
82
+ "VERY IMPORTANT: Deliver all responses strictly as plain text sentences. You must avoid using bullet points, lists, bolding, italics, or any similar special formatting."
83
+ ))
84
+ if not messages:
85
+ return state
86
+ response = llm_with_tools.invoke([sys_msg] + messages)
87
+ state["messages"] = state["messages"] + [response]
88
+ return state
89
+
90
+
91
+ # FastAPI setup (optional if you plan to use it as a web service)
92
+ app = FastAPI()
93
+
94
+ class ChatInput(BaseModel):
95
+ message: str
96
+
97
+ # Build Graph
98
+ builder = StateGraph(MessagesState)
99
+ builder.add_node("prompt_enhancer", prompt_enhancer)
100
+ builder.add_node("assistant",assistant)
101
+ builder.add_node("tools",ToolNode(tools))
102
+
103
+ # first run the enhancer
104
+ builder.add_edge(START,"prompt_enhancer")
105
+ builder.add_edge("prompt_enhancer", "assistant")
106
+ builder.add_conditional_edges("assistant", tools_condition)
107
+ builder.add_edge("tools","assistant")
108
+
109
+ memory = MemorySaver()
110
+ react_graph = builder.compile(checkpointer=memory)
111
+
112
+ # replace your existing /invoke handler with this:
113
+ @app.post("/chat")
114
+ async def chat(input: ChatInput):
115
+ config = {"configurable": {"thread_id": "1"}}
116
+ inputs = {"messages": [HumanMessage(content=input.message)]}
117
+ resp = react_graph.invoke(inputs, config)
118
+ last = resp.get("messages", [])[-1]
119
+ return JSONResponse({"response": last.content})
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ fastapi
2
+ uvicorn[standard]
3
+ langchain-core
4
+ langgraph
5
+ langchain-tavily
6
+ langchain-experimental
7
+ langchain-ollama
8
+ pydantic