Sathwik3 commited on
Commit
a2c0b81
·
verified ·
1 Parent(s): 7c29c1e

first commit , uploading 3 files

Browse files
Files changed (3) hide show
  1. Dockerfile +21 -0
  2. app.py +113 -0
  3. requirements.txt +73 -0
Dockerfile ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Start with an official Python base image
2
+ FROM python:3.10-slim
3
+
4
+ # Set the working directory inside the container
5
+ WORKDIR /app
6
+
7
+ # Copy your project's requirements file
8
+ COPY requirements.txt .
9
+
10
+ # Install the Python libraries
11
+ RUN pip install --no-cache-dir -r requirements.txt
12
+
13
+ # Copy the rest of your application's code
14
+ COPY . .
15
+
16
+ # Expose the port your app will run on (e.g., if it's an API)
17
+ EXPOSE 8000
18
+
19
+ # Define the command to run your application
20
+ # This assumes your main script is named "app.py"
21
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,113 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ==============================================================================
2
+ # 1. SETUP AND IMPORTS
3
+ # ==============================================================================
4
+ import os
5
+ from fastapi import FastAPI
6
+ from pydantic import BaseModel
7
+ from typing import Optional
8
+
9
+ # Set Google API Key securely from an environment variable
10
+ google_api_key = os.getenv("GOOGLE_API_KEY")
11
+ if not google_api_key:
12
+ raise ValueError("Google API key not found. Please set the GOOGLE_API_KEY environment variable.")
13
+
14
+ # All your other imports...
15
+ import bs4
16
+ from langchain import hub
17
+ from langchain_community.document_loaders import WebBaseLoader
18
+ from langchain_text_splitters import RecursiveCharacterTextSplitter
19
+ from langchain_google_genai import ChatGoogleGenerativeAI, GoogleGenerativeAIEmbeddings
20
+ from langchain_core.vectorstores import InMemoryVectorStore
21
+ from langgraph.graph import MessagesState, StateGraph, END
22
+ from langgraph.prebuilt import ToolNode, tools_condition
23
+ from langchain_core.messages import HumanMessage
24
+ from langchain_core.tools import tool
25
+ from langgraph.checkpoint.memory import MemorySaver
26
+
27
+ # ==============================================================================
28
+ # 2. CORE LOGIC
29
+ # ==============================================================================
30
+ llm = ChatGoogleGenerativeAI(model="gemini-2.5-flash", google_api_key=google_api_key)
31
+ embeddings = GoogleGenerativeAIEmbeddings(model="models/embedding-001", google_api_key=google_api_key)
32
+ vector_store = InMemoryVectorStore(embeddings)
33
+
34
+ # Load Web Data and Split
35
+ web_url = "https://lilianweng.github.io/posts/2023-06-23-agent/"
36
+ loader = WebBaseLoader(
37
+ web_paths=(web_url,),
38
+ bs_kwargs=dict(parse_only=bs4.SoupStrainer(class_=("post-content", "post-title", "post-header")))
39
+ )
40
+ docs = loader.load()
41
+ text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=200, add_start_index=True)
42
+ all_splits = text_splitter.split_documents(docs)
43
+ vector_store.add_documents(all_splits)
44
+
45
+ # Tool Definition
46
+ @tool
47
+ def retrieve(query: str):
48
+ """retrieve information related to a query."""
49
+ retrieved_docs = vector_store.similarity_search(query, k=2)
50
+ return [doc.page_content for doc in retrieved_docs]
51
+
52
+ # Graph Node Functions
53
+ def query_or_respond(state: MessagesState):
54
+ llm_with_tools = llm.bind_tools([retrieve])
55
+ response = llm_with_tools.invoke(state["messages"])
56
+ return {"messages": [response]}
57
+
58
+ tools = ToolNode([retrieve])
59
+
60
+ def generate(state: MessagesState):
61
+ response = llm.invoke(state["messages"])
62
+ return {"messages": [response]}
63
+
64
+ # Compile the LangGraph StateGraph
65
+ graph_builder = StateGraph(MessagesState)
66
+ graph_builder.add_node("query_or_respond", query_or_respond)
67
+ graph_builder.add_node("tools", tools)
68
+ graph_builder.add_node("generate", generate)
69
+ graph_builder.set_entry_point("query_or_respond")
70
+ graph_builder.add_conditional_edges(
71
+ "query_or_respond",
72
+ tools_condition,
73
+ {"tools": "tools", END: END}
74
+ )
75
+ graph_builder.add_edge("tools", "generate")
76
+ graph_builder.add_edge("generate", END)
77
+
78
+ memory = MemorySaver()
79
+ graph = graph_builder.compile(checkpointer=memory)
80
+
81
+ # ==============================================================================
82
+ # 3. API SERVER (Replaces your if __name__ == "__main__": block)
83
+ # ==============================================================================
84
+ app = FastAPI(
85
+ title="LangGraph RAG Agent Server",
86
+ description="An API server for a RAG agent built with LangGraph.",
87
+ )
88
+
89
+ # Define the input model for the API
90
+ class UserRequest(BaseModel):
91
+ message: str
92
+ thread_id: Optional[str] = "default_thread" # Use a default thread_id if none is provided
93
+
94
+ # Define the API endpoint
95
+ @app.post("/invoke")
96
+ async def invoke_agent(request: UserRequest):
97
+ # Set up the configuration for memory
98
+ config = {"configurable": {"thread_id": request.thread_id}}
99
+
100
+ # Define the input for the graph
101
+ inputs = {"messages": [HumanMessage(content=request.message)]}
102
+
103
+ # Invoke the graph to get the final result
104
+ response = graph.invoke(inputs, config=config)
105
+
106
+ # Return the AI's final message
107
+ final_message = response["messages"][-1]
108
+ return {"response": final_message.content}
109
+
110
+ # This part is for local testing, can be removed if using a production server
111
+ if __name__ == "__main__":
112
+ import uvicorn
113
+ uvicorn.run(app, host="0.0.0.0", port=8000)
requirements.txt ADDED
@@ -0,0 +1,73 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ aiohappyeyeballs==2.6.1
2
+ aiohttp==3.13.0
3
+ aiosignal==1.4.0
4
+ annotated-types==0.7.0
5
+ anyio==4.11.0
6
+ async-timeout==4.0.3
7
+ attrs==25.4.0
8
+ beautifulsoup4==4.14.2
9
+ cachetools==6.2.0
10
+ certifi==2025.10.5
11
+ charset-normalizer==3.4.3
12
+ click==8.1.8
13
+ dataclasses-json==0.6.7
14
+ exceptiongroup==1.3.0
15
+ fastapi==0.119.0
16
+ filetype==1.2.0
17
+ frozenlist==1.8.0
18
+ google-ai-generativelanguage==0.7.0
19
+ google-api-core==2.26.0
20
+ google-auth==2.41.1
21
+ googleapis-common-protos==1.70.0
22
+ grpcio==1.75.1
23
+ grpcio-status==1.75.1
24
+ h11==0.16.0
25
+ httpcore==1.0.9
26
+ httpx==0.28.1
27
+ httpx-sse==0.4.3
28
+ idna==3.10
29
+ jsonpatch==1.33
30
+ jsonpointer==3.0.0
31
+ langchain==0.3.27
32
+ langchain-community==0.3.31
33
+ langchain-core==0.3.79
34
+ langchain-google-genai==2.1.12
35
+ langchain-text-splitters==0.3.11
36
+ langgraph==0.6.10
37
+ langgraph-checkpoint==2.1.2
38
+ langgraph-prebuilt==0.6.4
39
+ langgraph-sdk==0.2.9
40
+ langsmith==0.4.34
41
+ marshmallow==3.26.1
42
+ multidict==6.7.0
43
+ mypy_extensions==1.1.0
44
+ numpy==2.0.2
45
+ orjson==3.11.3
46
+ ormsgpack==1.11.0
47
+ packaging==25.0
48
+ propcache==0.4.1
49
+ proto-plus==1.26.1
50
+ protobuf==6.32.1
51
+ pyasn1==0.6.1
52
+ pyasn1_modules==0.4.2
53
+ pydantic==2.12.0
54
+ pydantic-settings==2.11.0
55
+ pydantic_core==2.41.1
56
+ python-dotenv==1.1.1
57
+ PyYAML==6.0.3
58
+ requests==2.32.5
59
+ requests-toolbelt==1.0.0
60
+ rsa==4.9.1
61
+ sniffio==1.3.1
62
+ soupsieve==2.8
63
+ SQLAlchemy==2.0.44
64
+ starlette==0.48.0
65
+ tenacity==9.1.2
66
+ typing-inspect==0.9.0
67
+ typing-inspection==0.4.2
68
+ typing_extensions==4.15.0
69
+ urllib3==2.5.0
70
+ uvicorn==0.37.0
71
+ xxhash==3.6.0
72
+ yarl==1.22.0
73
+ zstandard==0.25.0