CoolFace
Apppublic

hughpearse/langgraph-serverless-multi-agentic-workflow

sourceHugging Facemitupdated 2y agoView on Hugging Face
0likes
app.py108 linesDownload Raw Back to root
1from langchain.prompts import PromptTemplate2from langchain.agents import create_react_agent, AgentExecutor, tool3from langchain.chains import RetrievalQA4from langgraph.graph import END, StateGraph, START5from langgraph.prebuilt import ToolNode6from langchain.output_parsers import ResponseSchema, StructuredOutputParser7from langchain.tools import Tool8from langchain_core.messages import BaseMessage, HumanMessage, AIMessage9from langchain_core.tools import tool10from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint11from huggingface_hub import login12from typing import Annotated, Dict, TypedDict, Optional13import gradio as gr14import os15import uuid16import json17from duckduckgo_search import DDGS18from itertools import islice19from pydantic import BaseModel, Field20import datetime21 22login(os.environ['HUGGINGFACE_HUB_API_KEY'])23 24def get_llm():25    return HuggingFaceEndpoint(26        repo_id="mistralai/Mistral-7B-Instruct-v0.3",27        huggingfacehub_api_token=os.environ['HUGGINGFACE_HUB_API_KEY'],28        temperature=0.7,29        task="text-generation",30        max_new_tokens=102431    )32 33class GraphState(TypedDict):34    question: Optional[str] = None35    next: Optional[str] = None36    response: Optional[str] = None37 38def agent_search_web_news(state: GraphState):39    llm = get_llm()40    prompt = PromptTemplate.from_template(41        "Generate exactly one short phrase, no more than 10 words,to search the web based on this input: {input}"42    )43    chain = prompt | llm44    search_phrase = chain.invoke({"input": state["question"]})45    results = DDGS().news(search_phrase, max_results=5)46    output = json.dumps(results)47    return {"response": [output]}48 49def agent_answer_code_question(state: GraphState):50    llm = get_llm()51    prompt = PromptTemplate.from_template(52        "You are a software engineer. Answer this question with step by steps details : {input}"53    )54    chain = prompt | llm55    response = chain.invoke({"input": state["question"]})56    return {"response": [response]}57 58def agent_answer_generic_question(state: GraphState):59    llm = get_llm()60    prompt = PromptTemplate.from_template(61        "Give a general and concise answer to the question: {input}"62    )63    chain = prompt | llm64    response = chain.invoke({"input": state["question"]})65    return {"response": [response]}66 67def agent_supervisor(state: GraphState):68    llm = get_llm()69    response_schemas = [70        ResponseSchema(name="next", description="classify as either 'generic', 'search_news' or 'programming'"),71    ]72    output_parser = StructuredOutputParser.from_response_schemas(response_schemas)73    format_instructions = output_parser.get_format_instructions()74    prompt = PromptTemplate(75        template="You are a classifier.\n{format_instructions}\n{input}",76        input_variables=["question"],77        partial_variables={"format_instructions": format_instructions},78    )79    chain = prompt | llm | output_parser80    response = chain.invoke({"input": state["question"]})81    state["next"] = response["next"]82    return state83 84def build_graph():85    workflow = StateGraph(GraphState)86    workflow.add_node("supervisor", agent_supervisor)87    workflow.add_node("coding", agent_answer_code_question)88    workflow.add_node("generic", agent_answer_generic_question)89    workflow.add_node("search_news", agent_search_web_news)90    workflow.add_edge(START, "supervisor")91    workflow.add_conditional_edges("supervisor", lambda state: state["next"])92    workflow.add_edge("coding", END)93    workflow.add_edge("generic", END)94    workflow.add_edge("search_news", END)95    app = workflow.compile()96    return app97 98app = build_graph()99def run_graph(input_message):100    inputs = {"question": input_message}101    response = app.invoke(inputs)102    return json.dumps(response, indent=2)103 104inputs = gr.Textbox(lines=2, placeholder="Enter your query here...")105outputs = gr.Textbox()106demo = gr.Interface(fn=run_graph, inputs=inputs, outputs=outputs, concurrency_limit=1)107demo.launch()108