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
/Users/harrisonchase/.pyenv/versions/3.9.1/envs/langchain/lib/python3.9/site-packages/deeplake/util/check_latest_version.py:32: UserWarning: A newer version of deeplake (3.6.14) is available. It's recommended that you update to the latest version using `pip install -U deeplake`.
warnings.warn(

Initialize Model

You can initialize this wrapper the same way you’d initialize ChatAnthropic

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

Passing in functions

You can now pass in functions in a similar way

functions = [
{
"name": "get_current_weather",
"description": "Get the current weather in a given location",
"parameters": {
"type": "object",
"properties": {
"location": {
"type": "string",
"description": "The city and state, e.g. 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

You can now use this 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

You can now use this 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'}