Spaces:
Sleeping
Sleeping
Update app.py
Browse files11 unmodified lines12131415161718191 unmodified line212223242526272829303132333435363738394041424344454647484950517 unmodified lines596061353637626364656667434445464748495051525354555657585960616263686970109 unmodified lines18018118217918018318418518611 unmodified linesfrom langgraph.graph import StateGraph, START, ENDfrom langgraph.graph.message import add_messagesfrom youtube_transcript_api import YouTubeTranscriptApifrom langchain.schema import AIMessagefrom huggingface_hub import InferenceClient
load_dotenv()
1 unmodified line# --- Constants ---DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
class HFInferenceLLM: def __init__(self): # Uses HF token automatically from the logged-in user or HF Space self.client = InferenceClient( model="meta-llama/Meta-Llama-3-8B-Instruct", token=os.getenv("HF_TOKEN") # Optional, auto-detected in HF Spaces ) def invoke(self, messages): # Extract content from messages if isinstance(messages, list) and len(messages) > 0: content = messages[0].content if hasattr(messages[0], 'content') else str(messages[0]) else: content = str(messages) # Generate response response = self.client.text_generation( content, max_new_tokens=500, temperature=0.1, do_sample=True, ) return AIMessage(content=response)
# --- LangGraph Agent State ---class AgentState(TypedDict): messages: Annotated[list, add_messages]7 unmodified linesclass BasicAgent: def __init__(self): print("LangGraph Agent initialized.") groq_client = Groq( api_key=os.getenv("GROQ_API_KEY") # Set this in HF Space secrets ) self.llm = HFInferenceLLM() # Initialize search tool self.search = DuckDuckGoSearchRun() # Build the LangGraph workflow self.workflow = self._build_workflow() self.app = self.workflow.compile()
class LLMWrapper: def invoke(self, messages): # Extract content from HumanMessage if isinstance(messages, list) and len(messages) > 0: content = messages[0].content if hasattr(messages[0], 'content') else str(messages[0]) else: content = str(messages) # Call Groq response = groq_client.chat.completions.create( model="llama-3.1-8b-instant", messages=[ {"role": "system", "content": "You are a helpful assistant."}, {"role": "user", "content": content} ], temperature=0.1 ) return response.choices[0].message.content
self.llm = LLMWrapper() # # Initialize the language model # self.llm = ChatOllama(109 unmodified lines response = self.llm.invoke([message])
# Extract only the FINAL ANSWER part # full_response = response.content full_response = response full_response = response.content final_answer = full_response
if "FINAL ANSWER:" in full_response:app.py+29-26