Spaces:
Build error
Build error
File size: 2,013 Bytes
d63c9ee |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
from tools import kg_search
from tools.kg_search import lookup_kg
from langchain.agents import AgentExecutor, create_react_agent
from langchain_core.prompts import PromptTemplate, ChatPromptTemplate, MessagesPlaceholder
from langchain.chains.conversation.memory import ConversationBufferMemory
from langchain.agents import Tool
from utils.utils import init_
from langchain_community.chat_message_histories import ChatMessageHistory
from langchain_core.runnables.history import RunnableWithMessageHistory
kg_query = Tool(
name = 'Query Knowledge Graph',
func = lookup_kg,
description='Useful for when you need to answer questions about job posts.'
)
tools = [kg_query]
with open("prompts/react_prompt.txt", "r") as file:
react_template = file.read()
react_prompt = PromptTemplate(
input_variables = ["tools", "tool_names", "input", "agent_scratchpad"],
template = react_template
)
prompt = ChatPromptTemplate.from_messages([
react_template,
MessagesPlaceholder(variable_name = "chat_history")
])
_, llm = init_()
# Init ReAct agent
agent = create_react_agent(llm, tools, react_prompt)
agent_executor = AgentExecutor(
agent = agent,
tools = tools,
verbose = True
)
message_history = ChatMessageHistory()
agent_with_chat_history = RunnableWithMessageHistory(
agent_executor,
lambda session_id : message_history,
input_messages_key = "input",
history_messages_key = "chat_history"
)
if __name__ == "__main__":
# Test ReAct Agent
question = {
"input": "Have any company recruit Machine Learning jobs?"
}
result = agent_with_chat_history.invoke(
question,
config = {"configurable": {"session_id": "foo"}}
)
print(result)
print("Answered!!!!!!!!")
# Test memory
question = {
"input": "What did I just ask?"
}
result = agent_with_chat_history.invoke(
question,
config={"configurable": {"session_id": "foo"}}
)
print(result)
x = input("> ")
|