Skip to main content

Anthropic Functions

This notebook shows how to use an experimental wrapper around Anthropic that gives it the same API as OpenAI Functions.

from langchain_experimental.llms.anthropic_functions import AnthropicFunctions

初始化模型 (Initialize Model)

您可以像初始化 ChatAnthropic 一样初始化此包装器。

model = AnthropicFunctions(model='claude-2')

传递函数 (Passing in functions)

现在您可以以类似的方式传递函数。

functions=[
{
"name": "get_current_weather",
"description": "获取给定位置的当前天气",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "城市和州,例如 San Francisco, CA"
},
"unit": {
"type": "string",
"enum": ["celsius", "fahrenheit"]
}
},
"required": ["location"]
}
}
]
from langchain.schema import HumanMessage
response = model.predict_messages(
[HumanMessage(content="whats the weater in boston?")],
functions=functions
)
response
    AIMessage(content=' ', additional_kwargs={'function_call': {'name': 'get_current_weather', 'arguments': '{"location": "Boston, MA", "unit": "fahrenheit"}'}}, example=False)

用于提取 (Using for extraction)

现在您可以使用它进行提取。

from langchain.chains import create_extraction_chain
schema = {
"properties": {
"name": {"type": "string"},
"height": {"type": "integer"},
"hair_color": {"type": "string"},
},
"required": ["name", "height"],
}
inp = """
Alex is 5 feet tall. Claudia is 1 feet taller Alex and jumps higher than him. Claudia is a brunette and Alex is blonde.
"""
chain = create_extraction_chain(schema, model)
chain.run(inp)
    [{'name': 'Alex', 'height': '5', 'hair_color': 'blonde'},
{'name': 'Claudia', 'height': '6', 'hair_color': 'brunette'}]

用于标记 (Using for tagging)

现在您可以使用它进行标记。

from langchain.chains import create_tagging_chain
schema = {
"properties": {
"sentiment": {"type": "string"},
"aggressiveness": {"type": "integer"},
"language": {"type": "string"},
}
}
chain = create_tagging_chain(schema, model)
chain.run("this is really cool")
    {'sentiment': 'positive', 'aggressiveness': '0', 'language': 'english'}