"""LangGraph single-node graph template. Returns a predefined response. Replace logic and configuration as needed. """ from __future__ import annotations from dataclasses import dataclass from typing import Any, Dict from langgraph.graph import StateGraph from langgraph.runtime import Runtime from typing_extensions import TypedDict import logging import asyncio from langchain_huggingface import ChatHuggingFace, HuggingFaceEndpoint from langchain_core.messages import HumanMessage logging.basicConfig(level=logging.INFO) logger = logging.getLogger(__name__) class Context(TypedDict): """Context parameters for the agent. Set these when creating assistants OR when invoking the graph. See: https://langchain-ai.github.io/langgraph/cloud/how-tos/configuration_cloud/ """ hf_token: str my_configurable_param: str @dataclass class State: """Input state for the agent. Defines the initial structure of incoming data. See: https://langchain-ai.github.io/langgraph/concepts/low_level/#state """ messages: list = None changeme: str = "example" def __post_init__(self): if self.messages is None: self.messages = [] async def call_model(state: State, runtime: Runtime[Context]) -> Dict[str, Any]: """Process input and returns output. Can use runtime context to alter behavior. """ hf_token = runtime.context.get("hf_token") if not hf_token: raise ValueError("Hugging Face token is required but not provided in context") # Initialize the model with the token from runtime context # Using asyncio.to_thread to avoid blocking calls in async context llm = await asyncio.to_thread( HuggingFaceEndpoint, repo_id="meta-llama/Llama-3.1-8B-Instruct", huggingfacehub_api_token=hf_token, ) model = ChatHuggingFace(llm=llm) logger.info(f"State:{state}") messages = state.messages + [HumanMessage(content=state.changeme)] content = "" async for chunk in model.astream(messages): logger.info(f"Chunk:{chunk}") if chunk.content: content += chunk.content logger.info(f"Response:{content}") messages.append({"role": "assistant", "content": content}) return {"messages": messages, "changeme": content} # Define the graph graph = ( StateGraph(State, context_schema=Context) .add_node(call_model) .add_edge("__start__", "call_model") .compile(name="New Graph") )