Giter VIP home page Giter VIP logo

captainfunction's Introduction

Welcome to CaptainFunction! ๐Ÿš€

CaptainFunction is a dynamic Python library for loading custom functions into OpenAI's powerful AI models. This toolkit makes integrating custom functionalities into OpenAI Assistants both easy and flexible.

I'm releasing this with two basic functions to start, a web search using Metaphor and a web scrape using BeautifulSoup. Both can be improved, but this way when I improve a function in one place, it improves everywhere I use it. You are more than welcome to contribute!

Getting Started ๐ŸŒŸ

Prerequisites

  • Python 3.6 or later.
  • Access to OpenAI's API (API key).
  • Other API keys depending on the function you load.

Installation

  1. Install CaptainFunction:
    pip install git+https://github.com/yoheinakajima/captainfunction.git
    

*Note, you currently need to separately install required libraries from each function you load.

Contributing Functions

Contribute your best functions in the functions directory. Each function should be in its separate file. The names get_function_schema() and handle_response() should not be changed. For example:

import os

def get_function_schema():
    return {
        "name": "function_name",
        "description": "What your function does.",
        "parameters": {
            "type": "object",
            "properties": {
                "argument1": {
                    "type": "string",
                    "description": "The first argument."
                },
                ...
            }
        }
    }

def handle_response(arguments):
    arguments = json.loads(arguments)
    argument1 = arguments["argument1"]
    # Your function logic here
    return "Function response"

Using CaptainFunction ๐Ÿ› ๏ธ

Loading Functions into OpenAI Assistant

First, load your functions using the load_functions method. For instance:

loaded_funcs = load_functions('web_search','web_content_scraper')

This loads the functions 'web_search' and 'web_content_scraper' from the functions directory.

Integrating with OpenAI Assistant

Create the function schemas and initialize the OpenAI assistant (this examples is for Assistants API, need to adjust for other endpoints):

function_schemas = [{"type": "function", "function": func['schema']} for func in loaded_funcs.values()]

assistant = openai_client.beta.assistants.create(
    instructions="Respond to user queries.",
    model="gpt-3.5-turbo",
    tools=function_schemas
)

Handling OpenAI API Calls

Handle the function calls in your application logic (this examples is for Assistants API, need to adjust for other endpoints):

# Wait for run to complete
while True:
    run_response = openai_client.beta.threads.runs.retrieve(
        thread_id=thread.id,
        run_id=run.id
    )
    if run_response.status == "completed":
        break
    elif run_response.status == "requires_action":
        # Handle function calls
        tool_outputs = []
        for tool_call in run_response.required_action.submit_tool_outputs.tool_calls:
            function_name = tool_call.function.name
            arguments = tool_call.function.arguments

            if function_name in loaded_funcs:
                handle_response_func = loaded_funcs[function_name]['handle_response']
                output = handle_response_func(arguments)
                tool_outputs.append({
                    "tool_call_id": tool_call.id,
                    "output": output,
                })

        openai_client.beta.threads.runs.submit_tool_outputs(
            thread_id=thread.id,
            run_id=run.id,
            tool_outputs=tool_outputs
        )
        pass
    time.sleep(1)  # Avoid spamming the API too quickly

Contributing ๐Ÿค

We encourage contributions! Feel free to add new functions or improve existing ones. To contribute:

  • Fork the repository.
  • Create a new branch for your contribution.
  • Add your new function or improvements.
  • Submit a pull request.

All contributions are welcome (including helping manage any repo, because I'm candidly not great at it).

License ๐Ÿ“„

MIT

captainfunction's People

Contributors

yoheinakajima avatar

Watchers

 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.