add agent.py
This commit is contained in:
@@ -1,39 +1,31 @@
|
|||||||
"""
|
"""
|
||||||
Agent definition using LangChain create_agent.
|
Agent creation with RAG integration.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from langchain_openai import ChatOpenAI
|
from langchain_openai import ChatOpenAI
|
||||||
from langchain.agents import create_agent
|
from langchain.agents import create_agent
|
||||||
from langchain_core.messages import HumanMessage
|
from langchain_core.messages import HumanMessage
|
||||||
|
from langgraph.checkpoint.memory import MemorySaver
|
||||||
from tools import search_knowledge_base, add_to_knowledge_base
|
from tools import search_knowledge_base, add_to_knowledge_base
|
||||||
|
|
||||||
# LLM – Ollama via BroJS endpoint (placeholder)
|
# LLM via Ollama (llama3)
|
||||||
llm = ChatOpenAI(
|
llm = ChatOpenAI(
|
||||||
model="openai/gpt-oss-20b:free",
|
model="ollama/llama3",
|
||||||
base_url="https://platform.brojs.ru/jrnl-bh/api/inference/v1",
|
base_url="http://localhost:11434/v1",
|
||||||
api_key=os.getenv("JOURNAL_MCP_PAT"),
|
api_key=None,
|
||||||
temperature=0.5,
|
temperature=0.2,
|
||||||
)
|
)
|
||||||
|
|
||||||
agent = create_agent(
|
agent = create_agent(
|
||||||
llm=llm,
|
llm=llm,
|
||||||
tools=[search_knowledge_base, add_to_knowledge_base],
|
tools=[search_knowledge_base, add_to_knowledge_base],
|
||||||
system_prompt="You are a helpful assistant with access to a knowledge base.",
|
system_prompt="You are a helpful assistant that can search and add documents to the knowledge base.",
|
||||||
)
|
)
|
||||||
|
|
||||||
async def main():
|
memory = MemorySaver()
|
||||||
# Simple demo: add and search
|
|
||||||
await agent.ainvoke(
|
|
||||||
{"messages": [HumanMessage(content="Add sample text about Python")]},
|
|
||||||
{"configurable": {"thread_id": "demo-1"}},
|
|
||||||
)
|
|
||||||
result = await agent.ainvoke(
|
|
||||||
{"messages": [HumanMessage(content="Search for Python")]},
|
|
||||||
{"configurable": {"thread_id": "demo-1"}},
|
|
||||||
)
|
|
||||||
print(result["messages"][-1].content)
|
|
||||||
|
|
||||||
if __name__ == "__main__": # pragma: no cover
|
async def run_agent(messages: List[HumanMessage]):
|
||||||
import asyncio
|
result = await agent.ainvoke({"messages": messages}, {"configurable": {"thread_id": "session-1"}})
|
||||||
asyncio.run(main())
|
return result["messages"][-1].content
|
||||||
|
|||||||
Reference in New Issue
Block a user