Code Monkey home page Code Monkey logo

anthropic_with_functions's Introduction

Anthropic with Functions

This library allows you to use the Anthropic Claude models with OpenAI-like Functions.

It's super rough and early, so feel free to make improvements if you want!

Installation

You can install this package directly from GitHub:

pip install git+https://github.com/mshumer/anthropic_with_functions.git

Usage

Here's a basic usage example:

from anthropic_function import AnthropicFunction
import json

anthropic_func = AnthropicFunction(api_key="ANTHROPIC_API_KEY", model="claude-2", temperature=0.7, max_tokens_to_sample=500)

# Define your functions
def get_current_weather(location, unit="fahrenheit"):
  # Get the current weather in a given location
  weather_info = {
      "location": location,
      "temperature": "72", # hardcoded for the example
      "unit": unit,
      "forecast": ["sunny", "windy"], # hardcoded for the example
  }
  return json.dumps(weather_info)

# Add your functions to the AnthropicFunction instance
anthropic_func.add_function(
    "get_current_weather", "Get the current weather in a given location",
    ["location: string", "unit: 'celsius' | 'fahrenheit'"])

# Define the conversation messages
messages = [{"role": "HUMAN", "content": "how are you today?"}, {"role": "AI", "content": "I'm good, thanks for asking!"}, {"role": "HUMAN", "content": "Remind me what I just asked you?"}, {"role": "AI", "content": "You just asked me, how are you today? and I responded, I'm good, thanks for asking!"}, {"role": "HUMAN", "content": "What's the weather in London?"}]

# Call the model (it will return either a function or a normal message)
response = anthropic_func.call(messages, model="claude-2", temperature=0.8, max_tokens_to_sample=400)

if response["function"]:
  # Parse and then call the function with the arguments
  function_output = None

  # Depending on your function(s), write parsing code to grab the function name and arguments
  #### PARSING CODE GOES HERE
  function_name = 'get_current_weather' # placeholder -- replace with your parsing code that grabs the function name
  function_arguments = {'location': 'london', 'unit': 'celsius'} # placeholder -- replace with your parsing code that grabs the function arguments

  # Now, call the relevant function with the arguments, return the result as `function_output`
  if function_name == 'get_current_weather':
    function_output = get_current_weather(location=function_arguments['location'], unit=function_arguments['unit'])
  # Describe the function's output
  if function_output is not None:
      response = anthropic_func.describe_function_output(function_name, function_arguments, function_output, messages)
      print('Response:', response['response'])

else:
  print('No function found')
  print('Response:', response['response'])

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Some ideas:

  • create automatic function / arguments parsing code so that the user doesn't need to write it themselves
  • generally get the library to parity w/ OpenAI's Functions system

License

This project is licensed under the terms of the MIT license.

anthropic_with_functions's People

Contributors

mshumer avatar joshbickett avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.