{"id":19222634,"url":"https://github.com/jakecyr/openai-function-calling","last_synced_at":"2025-04-16T01:09:22.124Z","repository":{"id":176468865,"uuid":"658086611","full_name":"jakecyr/openai-function-calling","owner":"jakecyr","description":"Helper functions to generate JSON schema dicts for OpenAI ChatGPT function calling requests.","archived":false,"fork":false,"pushed_at":"2025-03-07T01:09:48.000Z","size":168,"stargazers_count":76,"open_issues_count":0,"forks_count":13,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-16T01:08:59.033Z","etag":null,"topics":["chatgpt","function-calling","machine-learning","openai","python"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jakecyr.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-06-24T18:12:51.000Z","updated_at":"2025-04-14T05:10:11.000Z","dependencies_parsed_at":"2023-12-15T05:27:18.867Z","dependency_job_id":"9440d3e2-5f69-484f-b2f5-5223555c072d","html_url":"https://github.com/jakecyr/openai-function-calling","commit_stats":null,"previous_names":["jakecyr/openai-function-calling"],"tags_count":18,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakecyr%2Fopenai-function-calling","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakecyr%2Fopenai-function-calling/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakecyr%2Fopenai-function-calling/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jakecyr%2Fopenai-function-calling/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jakecyr","download_url":"https://codeload.github.com/jakecyr/openai-function-calling/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249178212,"owners_count":21225349,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatgpt","function-calling","machine-learning","openai","python"],"created_at":"2024-11-09T15:05:01.787Z","updated_at":"2025-04-16T01:09:22.092Z","avatar_url":"https://github.com/jakecyr.png","language":"Python","readme":"# OpenAI Function Calling\n\n[![GitHub Actions Build Status](https://github.com/jakecyr/openai-function-calling/actions/workflows/test-application.yml/badge.svg)](https://github.com/jakecyr/openai-function-calling/actions)\n[![PyPi Package Version](https://badge.fury.io/py/openai-function-calling.svg)](https://pypi.org/project/openai-function-calling/)\n\nHelper functions to generate JSON schema dicts for OpenAI ChatGPT function calling requests. See the [official Function Calling reference](https://platform.openai.com/docs/guides/gpt/function-calling) for more information.\n\n## Installation\n\nInstall from PyPi with:\n\n```bash\npip install openai-function-calling\n```\n\n**The openai-function-calling package does come with the openai package. It must be installed separately with `pip install openai`**\n\n## Usage\n\n### Auto-Infer the Function Definition (Beta)\n\nAutomatically infer your function name, description, and parameters given a reference to the function. A `Function` instance is returned which can be converted to JSON schema with `.to_json_schema()` and then passed to the OpenAI chat completion API:\n\n```python\nfrom typing import Any, Callable\nfrom openai_function_calling import FunctionInferrer\nimport openai\nimport json\n\n# Define example functions.\n\ndef get_current_weather(location: str, unit: str = \"fahrenheit\") -\u003e str:\n    \"\"\"Get the current weather and return a summary.\"\"\"\n    return f\"It is currently sunny in {location} and 75 degrees {unit}.\"\n\n\ndef get_tomorrows_weather(location: str, unit: str = \"fahrenheit\") -\u003e str:\n    \"\"\"Get the weather for tomorrow and return a summary.\"\"\"\n    return f\"Tomorrow it will be rainy in {location} and 60 degrees {unit}.\"\n\n# Infer the function definitions.\nget_current_weather_function = FunctionInferrer.infer_from_function_reference(\n    get_current_weather\n)\n\nget_tomorrows_weather_function = FunctionInferrer.infer_from_function_reference(\n    get_tomorrows_weather\n)\n\n# Get the function to call from ChatGPT (you would normally have more than one).\nresponse = openai.ChatCompletion.create(\n    model=\"gpt-3.5-turbo-0613\",\n    messages=[\n        {\n            \"role\": \"user\",\n            \"content\": \"What will the weather be like in Boston, MA today?\",\n        }\n    ],\n    functions=[\n        # Convert the functions to JSON schema.\n        get_current_weather_function.to_json_schema(),\n        get_tomorrows_weather_function.to_json_schema(),\n    ],\n)\n```\n\n### Define Functions with Objects\n\nDefine your function definitions using typed classes `Function` and `Parameter` which automatically convert to JSON schema with `.to_json_schema` methods. See an example below:\n\n```python\nfrom openai_function_calling import Function, FunctionDict, Parameter, JsonSchemaType\n\n\ndef get_current_weather(location: str, unit: str) -\u003e str:\n    \"\"\"Do some stuff in here.\"\"\"\n\n\n# Define the function.\nget_current_weather_function = Function(\n    \"get_current_weather\",\n    \"Get the current weather\",\n    [\n        Parameter(\n            name=\"location\",\n            type=JsonSchemaType.STRING,\n            description=\"The city and state, e.g. San Francisco, CA\",\n        ),\n        Parameter(\n            name=\"unit\",\n            type=JsonSchemaType.STRING,\n            description=\"The temperature unit to use.\",\n            enum=[\"celsius\", \"fahrenheit\"],\n        ),\n    ],\n)\n\n# Convert to a JSON schema dict to send to OpenAI.\nget_current_weather_function_schema = get_current_weather_function.to_json_schema()\n```\n\n### Convert Functions to OpenAI Compatible JSON\n\n```python\nfrom openai import OpenAI\nfrom openai.types.chat import (\n    ChatCompletion,\n    ChatCompletionUserMessageParam,\n)\nfrom openai_function_calling.tool_helpers import ToolHelpers\n\n\n# Define our functions.\ndef get_current_weather(location: str, unit: str) -\u003e str:\n    \"\"\"Get the current weather and return a summary.\"\"\"\n    return f\"It is currently sunny in {location} and 75 degrees {unit}.\"\n\n\ndef get_tomorrows_weather(location: str, unit: str) -\u003e str:\n    \"\"\"Get tomorrow's weather and return a summary.\"\"\"\n    return f\"It will be rainy tomorrow in {location} and around 65 degrees {unit}.\"\n\n\nopenai_client = OpenAI()\n\n# Send the query and our function context to OpenAI.\nresponse: ChatCompletion = openai_client.chat.completions.create(\n    model=\"gpt-3.5-turbo-1106\",\n    messages=[\n        ChatCompletionUserMessageParam(\n            role=\"user\", content=\"What's the weather in Boston MA?\"\n        ),\n    ],\n    tools=ToolHelpers.infer_from_function_refs(\n        [get_current_weather, get_tomorrows_weather]\n    ),\n    tool_choice=\"auto\",\n)\n```\n\n## Examples\n\nTo run the examples, set the environment variable `OPENAI_API_KEY` to your OpenAI API key. For example:\n\n```bash\nexport OPENAI_API_KEY=SOME_KEY_VALUE\n\n# or when running an example\n\nOPENAI_API_KEY=SOME_KEY_VALUE python examples/weather_functions.py\n```\n\nMake sure to also follow all instructions in the [Installation section](#installation).\n\nSee complete examples in the [./examples](https://github.com/jakecyr/openai-function-calling/tree/master/examples) folder.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjakecyr%2Fopenai-function-calling","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjakecyr%2Fopenai-function-calling","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjakecyr%2Fopenai-function-calling/lists"}