In [ ]
已复制!
%pip install llama-index-llms-openai
%pip install llama-index-agents-openai
%pip install llama-index-llms-openai %pip install llama-index-agents-openai
In [ ]
已复制!
from llama_index.core.tools import FunctionTool
from llama_index.agent.openai import OpenAIAgent
from llama_index.llms.openai import OpenAI
import os
from llama_index.core.tools import FunctionTool from llama_index.agent.openai import OpenAIAgent from llama_index.llms.openai import OpenAI import os
In [ ]
已复制!
os.environ["OPENAI_API_KEY"] = "sk-"
os.environ["OPENAI_API_KEY"] = "sk-"
此函数向用户显示为函数调用生成的数据,并请求用户输入以返回交互。
In [ ]
已复制!
def callback(message):
confirmation = input(
f"{message[1]}\nDo you approve of sending this greeting?\nInput(Y/N):"
)
if confirmation.lower() == "y":
# Here you can trigger an action such as sending an email, message, api call, etc.
return "Greeting sent successfully."
else:
return (
"Greeting has not been approved, talk a bit about how to improve"
)
def callback(message): confirmation = input( f"{message[1]}\n你批准发送此问候语吗?\n输入(Y/N):" ) if confirmation.lower() == "y": # 你可以在此处触发诸如发送电子邮件、消息、API 调用等操作。 return "问候语发送成功。" else: return ( "问候语未获批准,请谈谈如何改进" )
只需要收件人和问候消息的简单函数。
In [ ]
已复制!
def send_hello(destination: str, message: str) -> str:
"""
Say hello with a rhyme
destination: str - Name of recipient
message: str - Greeting message with a rhyme to the recipient's name
"""
return destination, message
hello_tool = FunctionTool.from_defaults(fn=send_hello, callback=callback)
def send_hello(destination: str, message: str) -> str: """ 使用押韵的问候语 destination: str - 收件人姓名 message: str - 包含与收件人姓名押韵的问候消息 """ return destination, message hello_tool = FunctionTool.from_defaults(fn=send_hello, callback=callback)
In [ ]
已复制!
hello_tool.to_langchain_tool
hello_tool.to_langchain_tool
Out [ ]
<bound method FunctionTool.to_langchain_tool of <llama_index.core.tools.function_tool.FunctionTool object at 0x7f7da9fa5670>>
In [ ]
已复制!
llm = OpenAI()
agent = OpenAIAgent.from_tools([hello_tool])
llm = OpenAI() agent = OpenAIAgent.from_tools([hello_tool])
In [ ]
已复制!
response = agent.chat("Send hello to Karen")
print(str(response))
response = agent.chat("向 Karen 发送问候") print(str(response))
The hello message has been sent to Karen with the rhyme "Hello Karen, you're a star!"
In [ ]
已复制!
response = agent.chat("Send hello to Joe")
print(str(response))
response = agent.chat("向 Joe 发送问候") print(str(response))
I have successfully sent a hello message to Joe with the greeting "Hello Joe, you're a pro!"