Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] OpenAI Function Calling #1224

Merged
merged 11 commits into from
Feb 12, 2024
Merged

[Feature] OpenAI Function Calling #1224

merged 11 commits into from
Feb 12, 2024

Conversation

maccuryj
Copy link
Contributor

@maccuryj maccuryj commented Jan 27, 2024

Description

This pull request includes primarily the implementation of a solution for OpenAI function calling.

To achieve better consistency in the OpenAILlm code, the Langchain package has been upgraded to version 0.1.4. In this context, a set of imports across the code has been amended for compatbility and preparation for incoming deprecations. No impact could be identified on the package functionality.

The Function calling has been implemented to allow for the passing of a single function structure to an OpenAILlm instance. This instance will be limited to trying to map the query to the provided function structure.
Pydantic models, Python functions and OpenAI format dicts are accepted as input.
When queried, the model returns a string, with either a JSON-formatted output of the method arguments, or a default message documenting the failure.

A test has been developed for the OpenAILlm querying functionality, when passing a "tools" object representing the function structure. The documentation has been adjusted to reflect the feature as now implemented.

It must be noted that the previous implementation which had been merged into the repository is faulty. It does not even leverage OpenAIs Function calling interface (it simply sends a normal query), nor does it result in the output that is expected from this feature (no method argument matching). It also made use of a legacy Langchain interface (function_call instead of tools_call).

Fixes #796

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Documentation update

How Has This Been Tested?

During development, a script running the functionality with different types of input was created and used. This also included negative tests. Furthermore, a pytest has been developed to make sure the function is executed as expected.

The functionality can be tested by running the code that has been added to the docs/components/llms.mdx file.

Please delete options that are not relevant.

  • Unit Test
  • Test Script (see updated documentation)

Test script:

"""
This file provides multiple input types to test the Function Calling functionality developed in the Embedchain project.
Simply add your OpenAI API key and uncomment the input type you wish to test and run the file.
"""

import os

from pydantic import BaseModel, Field

from embedchain import App
from embedchain.llm.openai import OpenAILlm

os.environ["OPENAI_API_KEY"] = "sk-xxx"


class multiply(BaseModel):
    """Multiply two integers together."""

    a: int = Field(..., description="First integer")
    b: int = Field(..., description="Second integer")


# def multiply(a: int, b: int) -> int:
#     """Multiply two integers together.

#     Args:
#         a: First integer
#         b: Second integer
#     """
#     return a * b


# multiply = {
#     "type": "function",
#     "function": {
#         "name": "multiply",
#         "description": "Multiply two integers together.",
#         "parameters": {
#             "type": "object",
#             "properties": {
#                 "a": {"description": "First integer", "type": "integer"},
#                 "b": {"description": "Second integer", "type": "integer"},
#             },
#             "required": ["a", "b"],
#         },
#     },
# }

llm = OpenAILlm(tools=multiply)
app = App(llm=llm)

result = app.query("What is the result of 125 multiplied by fifteen?")
print(result)

Checklist:

  • My code follows the style guidelines of this project

  • I have performed a self-review of my own code

  • I have made corresponding changes to the documentation

  • My changes generate no new warnings

  • I have added tests that prove my fix is effective or that my feature works

  • New and existing unit tests pass locally with my changes

  • I have checked my code and corrected any misspellings

Maintainer Checklist

  • closes #xxxx (Replace xxxx with the GitHub issue number)
  • Made sure Checks passed

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Jan 27, 2024
Copy link

codecov bot commented Jan 27, 2024

Codecov Report

Attention: 2 lines in your changes are missing coverage. Please review.

Comparison is base (d94aee8) 56.52% compared to head (48f354a) 56.75%.
Report is 2 commits behind head on main.

❗ Current head 48f354a differs from pull request most recent head 91cfc7e. Consider uploading reports for the commit 91cfc7e to get more accurate results

Files Patch % Lines
embedchain/loaders/rss_feed.py 0.00% 1 Missing ⚠️
embedchain/loaders/unstructured_file.py 0.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1224      +/-   ##
==========================================
+ Coverage   56.52%   56.75%   +0.22%     
==========================================
  Files         146      146              
  Lines        5935     5929       -6     
==========================================
+ Hits         3355     3365      +10     
+ Misses       2580     2564      -16     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@maccuryj
Copy link
Contributor Author

The attached script can be used to test the functionality with different inputs.
function_call_test.txt

@maccuryj maccuryj changed the title Langchain version upgrade + OpenAI Function Calling [Feature] OpenAI Function Calling Jan 28, 2024
Copy link
Collaborator

@deshraj deshraj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is awesome. Thanks for upgrading the version and fixing the function calling.

@dosubot dosubot bot added the lgtm This PR has been approved by a maintainer label Feb 12, 2024
@deshraj deshraj merged commit 41bd258 into mem0ai:main Feb 12, 2024
0 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lgtm This PR has been approved by a maintainer size:L This PR changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add support for OpenAI function calling
3 participants