Spaces:
Sleeping
Sleeping
Rajratan Gulab More commited on
Commit ยท
f9e678f
1
Parent(s): 311f1ea
'this'
Browse files- .history/app_20250322171124.yp +0 -0
- .history/app_20250322171127.yp +1 -0
- .history/app_20250322171131.py +0 -0
- .history/app_20250322171147.py +113 -0
- Pipfile +23 -0
- Pipfile.lock +0 -0
- app.py +113 -0
- requirements.txt +0 -0
.history/app_20250322171124.yp
ADDED
|
File without changes
|
.history/app_20250322171127.yp
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
p
|
.history/app_20250322171131.py
ADDED
|
File without changes
|
.history/app_20250322171147.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from typing import List
|
| 7 |
+
from langchain_groq import ChatGroq
|
| 8 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 9 |
+
from langgraph.prebuilt import create_react_agent
|
| 10 |
+
from langchain_core.messages.ai import AIMessage
|
| 11 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
+
from threading import Thread
|
| 13 |
+
import uvicorn
|
| 14 |
+
|
| 15 |
+
# ๐น Set API Keys
|
| 16 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 17 |
+
os.environ["TAVILY_API_KEY"] = "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"
|
| 18 |
+
|
| 19 |
+
# ๐น Initialize FastAPI
|
| 20 |
+
app = FastAPI(title="LangGraph AI Agent")
|
| 21 |
+
|
| 22 |
+
# ๐น Allow frontend to communicate with backend
|
| 23 |
+
app.add_middleware(
|
| 24 |
+
CORSMiddleware,
|
| 25 |
+
allow_origins=["*"],
|
| 26 |
+
allow_credentials=True,
|
| 27 |
+
allow_methods=["*"],
|
| 28 |
+
allow_headers=["*"],
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# ๐น Allowed model names
|
| 32 |
+
ALLOWED_MODEL_NAMES = ["llama3-70b-8192", "mixtral-8x7b-32768", "llama-3.3-70b-versatile", "gpt-4o-mini"]
|
| 33 |
+
|
| 34 |
+
# ๐น Request Schema
|
| 35 |
+
class RequestState(BaseModel):
|
| 36 |
+
model_name: str
|
| 37 |
+
model_provider: str
|
| 38 |
+
system_prompt: str
|
| 39 |
+
messages: List[str]
|
| 40 |
+
allow_search: bool
|
| 41 |
+
|
| 42 |
+
# ๐น AI Agent Function
|
| 43 |
+
def get_response_from_ai_agent(llm_id, query, allow_search, system_prompt, provider):
|
| 44 |
+
if provider == "Groq":
|
| 45 |
+
llm = ChatGroq(model=llm_id)
|
| 46 |
+
|
| 47 |
+
tools = [TavilySearchResults(max_results=5, api_key=os.environ["TAVILY_API_KEY"])] if allow_search else []
|
| 48 |
+
|
| 49 |
+
agent = create_react_agent(model=llm, tools=tools, state_modifier=system_prompt)
|
| 50 |
+
|
| 51 |
+
state = {"message": query}
|
| 52 |
+
response = agent.invoke(state)
|
| 53 |
+
|
| 54 |
+
messages = response.get("messages")
|
| 55 |
+
ai_messages = [message.content for message in messages if isinstance(message, AIMessage)]
|
| 56 |
+
|
| 57 |
+
return ai_messages[-1] if ai_messages else "No response from AI."
|
| 58 |
+
|
| 59 |
+
# ๐น FastAPI Route
|
| 60 |
+
@app.post("/chat")
|
| 61 |
+
def chat_endpoint(request: RequestState):
|
| 62 |
+
if request.model_name not in ALLOWED_MODEL_NAMES:
|
| 63 |
+
return {"error": "Invalid model name"}
|
| 64 |
+
|
| 65 |
+
response = get_response_from_ai_agent(
|
| 66 |
+
llm_id=request.model_name,
|
| 67 |
+
query=request.messages,
|
| 68 |
+
allow_search=request.allow_search,
|
| 69 |
+
system_prompt=request.system_prompt,
|
| 70 |
+
provider=request.model_provider
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return {"response": response}
|
| 74 |
+
|
| 75 |
+
# ๐น Run FastAPI in a separate thread
|
| 76 |
+
def run_fastapi():
|
| 77 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 78 |
+
|
| 79 |
+
Thread(target=run_fastapi, daemon=True).start()
|
| 80 |
+
|
| 81 |
+
# ๐น Streamlit UI
|
| 82 |
+
st.set_page_config(layout="wide", page_title="LangGraph AI Agent")
|
| 83 |
+
st.title("LangGraph AI Agent ๐ค")
|
| 84 |
+
st.markdown("### Chat with an AI Model Powered by LangGraph!")
|
| 85 |
+
|
| 86 |
+
# ๐น User Input Fields
|
| 87 |
+
system_prompt = st.text_area("Define your AI Agent:", placeholder="Type your prompt here...")
|
| 88 |
+
provider = st.selectbox("Select the model provider:", ["Groq", "OpenAI"])
|
| 89 |
+
model_name = st.selectbox(
|
| 90 |
+
"Select the model name:",
|
| 91 |
+
["llama3-70b-8192", "mixtral-8x7b-32768", "llama-3.3-70b-versatile"] if provider == "Groq" else ["gpt-4o-mini"]
|
| 92 |
+
)
|
| 93 |
+
allow_search = st.checkbox("Allow search")
|
| 94 |
+
user_query = st.text_area("Enter query:", placeholder="Type your query here...", height=150)
|
| 95 |
+
|
| 96 |
+
API_URL = "http://127.0.0.1:8000/chat"
|
| 97 |
+
|
| 98 |
+
if st.button("Chat ๐ค"):
|
| 99 |
+
if user_query.strip():
|
| 100 |
+
response = requests.post(API_URL, json={
|
| 101 |
+
"model_name": model_name,
|
| 102 |
+
"model_provider": provider,
|
| 103 |
+
"system_prompt": system_prompt,
|
| 104 |
+
"messages": [user_query],
|
| 105 |
+
"allow_search": allow_search
|
| 106 |
+
})
|
| 107 |
+
|
| 108 |
+
if response.status_code == 200:
|
| 109 |
+
response_data = response.json()
|
| 110 |
+
if "error" in response_data:
|
| 111 |
+
st.error(response_data["error"])
|
| 112 |
+
else:
|
| 113 |
+
st.success(f"**AI Agent Response:** {response_data['response']}")
|
Pipfile
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
[[source]]
|
| 2 |
+
url = "https://pypi.org/simple"
|
| 3 |
+
verify_ssl = true
|
| 4 |
+
name = "pypi"
|
| 5 |
+
|
| 6 |
+
[packages]
|
| 7 |
+
langchain = "*"
|
| 8 |
+
langchain-community = "*"
|
| 9 |
+
langchain-ollama = "*"
|
| 10 |
+
langchain-core = "*"
|
| 11 |
+
faiss-cpu = "*"
|
| 12 |
+
pdfplumber = "*"
|
| 13 |
+
langgraph = "*"
|
| 14 |
+
langchain-groq = "*"
|
| 15 |
+
pydantic = "*"
|
| 16 |
+
fastapi = "*"
|
| 17 |
+
uvicorn = "*"
|
| 18 |
+
streamlit = "*"
|
| 19 |
+
|
| 20 |
+
[dev-packages]
|
| 21 |
+
|
| 22 |
+
[requires]
|
| 23 |
+
python_version = "3.9"
|
Pipfile.lock
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
app.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import requests
|
| 4 |
+
from fastapi import FastAPI
|
| 5 |
+
from pydantic import BaseModel
|
| 6 |
+
from typing import List
|
| 7 |
+
from langchain_groq import ChatGroq
|
| 8 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 9 |
+
from langgraph.prebuilt import create_react_agent
|
| 10 |
+
from langchain_core.messages.ai import AIMessage
|
| 11 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 12 |
+
from threading import Thread
|
| 13 |
+
import uvicorn
|
| 14 |
+
|
| 15 |
+
# ๐น Set API Keys
|
| 16 |
+
os.environ["GROQ_API_KEY"] = "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"
|
| 17 |
+
os.environ["TAVILY_API_KEY"] = "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"
|
| 18 |
+
|
| 19 |
+
# ๐น Initialize FastAPI
|
| 20 |
+
app = FastAPI(title="LangGraph AI Agent")
|
| 21 |
+
|
| 22 |
+
# ๐น Allow frontend to communicate with backend
|
| 23 |
+
app.add_middleware(
|
| 24 |
+
CORSMiddleware,
|
| 25 |
+
allow_origins=["*"],
|
| 26 |
+
allow_credentials=True,
|
| 27 |
+
allow_methods=["*"],
|
| 28 |
+
allow_headers=["*"],
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
# ๐น Allowed model names
|
| 32 |
+
ALLOWED_MODEL_NAMES = ["llama3-70b-8192", "mixtral-8x7b-32768", "llama-3.3-70b-versatile", "gpt-4o-mini"]
|
| 33 |
+
|
| 34 |
+
# ๐น Request Schema
|
| 35 |
+
class RequestState(BaseModel):
|
| 36 |
+
model_name: str
|
| 37 |
+
model_provider: str
|
| 38 |
+
system_prompt: str
|
| 39 |
+
messages: List[str]
|
| 40 |
+
allow_search: bool
|
| 41 |
+
|
| 42 |
+
# ๐น AI Agent Function
|
| 43 |
+
def get_response_from_ai_agent(llm_id, query, allow_search, system_prompt, provider):
|
| 44 |
+
if provider == "Groq":
|
| 45 |
+
llm = ChatGroq(model=llm_id)
|
| 46 |
+
|
| 47 |
+
tools = [TavilySearchResults(max_results=5, api_key=os.environ["TAVILY_API_KEY"])] if allow_search else []
|
| 48 |
+
|
| 49 |
+
agent = create_react_agent(model=llm, tools=tools, state_modifier=system_prompt)
|
| 50 |
+
|
| 51 |
+
state = {"message": query}
|
| 52 |
+
response = agent.invoke(state)
|
| 53 |
+
|
| 54 |
+
messages = response.get("messages")
|
| 55 |
+
ai_messages = [message.content for message in messages if isinstance(message, AIMessage)]
|
| 56 |
+
|
| 57 |
+
return ai_messages[-1] if ai_messages else "No response from AI."
|
| 58 |
+
|
| 59 |
+
# ๐น FastAPI Route
|
| 60 |
+
@app.post("/chat")
|
| 61 |
+
def chat_endpoint(request: RequestState):
|
| 62 |
+
if request.model_name not in ALLOWED_MODEL_NAMES:
|
| 63 |
+
return {"error": "Invalid model name"}
|
| 64 |
+
|
| 65 |
+
response = get_response_from_ai_agent(
|
| 66 |
+
llm_id=request.model_name,
|
| 67 |
+
query=request.messages,
|
| 68 |
+
allow_search=request.allow_search,
|
| 69 |
+
system_prompt=request.system_prompt,
|
| 70 |
+
provider=request.model_provider
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
return {"response": response}
|
| 74 |
+
|
| 75 |
+
# ๐น Run FastAPI in a separate thread
|
| 76 |
+
def run_fastapi():
|
| 77 |
+
uvicorn.run(app, host="0.0.0.0", port=8000)
|
| 78 |
+
|
| 79 |
+
Thread(target=run_fastapi, daemon=True).start()
|
| 80 |
+
|
| 81 |
+
# ๐น Streamlit UI
|
| 82 |
+
st.set_page_config(layout="wide", page_title="LangGraph AI Agent")
|
| 83 |
+
st.title("LangGraph AI Agent ๐ค")
|
| 84 |
+
st.markdown("### Chat with an AI Model Powered by LangGraph!")
|
| 85 |
+
|
| 86 |
+
# ๐น User Input Fields
|
| 87 |
+
system_prompt = st.text_area("Define your AI Agent:", placeholder="Type your prompt here...")
|
| 88 |
+
provider = st.selectbox("Select the model provider:", ["Groq", "OpenAI"])
|
| 89 |
+
model_name = st.selectbox(
|
| 90 |
+
"Select the model name:",
|
| 91 |
+
["llama3-70b-8192", "mixtral-8x7b-32768", "llama-3.3-70b-versatile"] if provider == "Groq" else ["gpt-4o-mini"]
|
| 92 |
+
)
|
| 93 |
+
allow_search = st.checkbox("Allow search")
|
| 94 |
+
user_query = st.text_area("Enter query:", placeholder="Type your query here...", height=150)
|
| 95 |
+
|
| 96 |
+
API_URL = "http://127.0.0.1:8000/chat"
|
| 97 |
+
|
| 98 |
+
if st.button("Chat ๐ค"):
|
| 99 |
+
if user_query.strip():
|
| 100 |
+
response = requests.post(API_URL, json={
|
| 101 |
+
"model_name": model_name,
|
| 102 |
+
"model_provider": provider,
|
| 103 |
+
"system_prompt": system_prompt,
|
| 104 |
+
"messages": [user_query],
|
| 105 |
+
"allow_search": allow_search
|
| 106 |
+
})
|
| 107 |
+
|
| 108 |
+
if response.status_code == 200:
|
| 109 |
+
response_data = response.json()
|
| 110 |
+
if "error" in response_data:
|
| 111 |
+
st.error(response_data["error"])
|
| 112 |
+
else:
|
| 113 |
+
st.success(f"**AI Agent Response:** {response_data['response']}")
|
requirements.txt
ADDED
|
Binary file (9.91 kB). View file
|
|
|