Skip to content

Instantly share code, notes, and snippets.

@koduki
Last active January 28, 2024 08:39
Show Gist options
  • Save koduki/4ad41e265c8d23c4f2ae63644d1c1353 to your computer and use it in GitHub Desktop.
Save koduki/4ad41e265c8d23c4f2ae63644d1c1353 to your computer and use it in GitHub Desktop.
prompt_system = open("C:\\Users\\koduki\\git\\ai-tuber\\src\\backend\\prompt_system.txt", "r", encoding='utf-8').read()
prompt_for_chat = ChatPromptTemplate.from_messages([
("system", prompt_system),
("user", "{input}"),
MessagesPlaceholder(variable_name="agent_scratchpad"),
]).partial(format_instructions=parser.get_format_instructions())
prompt_for_tools = ChatPromptTemplate.from_messages([
("system", "You are agentai"),
("user", "{input}"),
])
tools = [weather_tool.weather_api, short_talk_tool.talk]
llm = ChatOpenAI(temperature=0, model='gpt-4-0613')
llm_with_tools = ChatOpenAI(temperature=0, model='gpt-3.5-turbo').bind(functions=[format_tool_to_openai_function(t) for t in tools])
agent = {
"input": lambda x: x["input"],
"agent_scratchpad": lambda x: format_to_openai_functions(x['intermediate_steps'])
} | prompt_for_chat | llm | OpenAIFunctionsAgentOutputParser()
agent_with_tools = {
"input": lambda x: x["input"],
} | prompt_for_tools | llm_with_tools | OpenAIFunctionsAgentOutputParser()
# Agent Loop 1
intermediate_steps = []
output = agent_with_tools.invoke({
"input": "今日の東京の天気は?",
})
print(output.tool, output.tool_input)
tool = next(x for x in tools if x.name == output.tool)
observation = tool.run(output.tool_input)
intermediate_steps.append((output, observation))
# Agent Loop 2
output = agent.invoke({
"input": "今日の東京の天気は?",
"intermediate_steps": intermediate_steps
})
output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment