Create agent.py
Browse files
agent.py
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain_openai import ChatOpenAI
|
| 2 |
+
from dotenv import load_dotenv
|
| 3 |
+
from os import getenv
|
| 4 |
+
# Load enviroment variables
|
| 5 |
+
load_dotenv()
|
| 6 |
+
grok_key = "gpt-4o-mini"
|
| 7 |
+
|
| 8 |
+
# Initialize OpenAI model
|
| 9 |
+
llm = ChatOpenAI(model= grok_key)
|
| 10 |
+
|
| 11 |
+
from typing import TypedDict, Annotated
|
| 12 |
+
from langgraph.graph import StateGraph, END, add_messages
|
| 13 |
+
class BasicChatState(TypedDict):
|
| 14 |
+
messages: Annotated[list, add_messages]
|
| 15 |
+
def chatbot(state: BasicChatState):
|
| 16 |
+
return {
|
| 17 |
+
"messages": [llm.invoke(state["messages"])]
|
| 18 |
+
}
|
| 19 |
+
# Create LangGraph nodes and edges
|
| 20 |
+
graph = StateGraph(BasicChatState)
|
| 21 |
+
graph.add_node("chatbot", chatbot)
|
| 22 |
+
graph.add_edge("chatbot", END)
|
| 23 |
+
|
| 24 |
+
# Set graph entry point
|
| 25 |
+
graph.set_entry_point("chatbot")
|
| 26 |
+
|
| 27 |
+
# Compile the graph
|
| 28 |
+
chat_agent = graph.compile()
|