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

Add tool calling to the LLM base class, implement in OpenAI #322

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

oskarhane
Copy link
Member

Description

Note

This PR adds tool calling functionality to the LLM base class with OpenAI implementation, enabling structured parameter extraction and function calling. The implementation includes both synchronous and asynchronous methods for tool invocation, with proper type hints and error handling. An example has been added to demonstrate how to use this new functionality.

Type of Change

  • New feature
  • Bug fix
  • Breaking change
  • Documentation update
  • Project configuration change

Complexity

Note

The implementation adds a new feature to the existing LLM interface but follows established patterns. It required careful handling of the OpenAI API's tool invocation parameters and proper type definitions.

Complexity: Medium

How Has This Been Tested?

  • Unit tests
  • E2E tests
  • Manual tests

Checklist

The following requirements should have been met (depending on the changes in the branch):

  • Documentation has been updated
  • Unit tests have been updated
  • E2E tests have been updated
  • Examples have been updated
  • New files have copyright header
  • CLA (https://neo4j.com/developer/cla/)) has been signed
  • CHANGELOG.md updated if appropriate

@oskarhane oskarhane requested a review from a team as a code owner April 4, 2025 09:16
@oskarhane oskarhane force-pushed the feature/llm-tool-invocation branch from cd47dc2 to 882915f Compare April 4, 2025 09:19
@oskarhane oskarhane force-pushed the feature/llm-tool-invocation branch from fc2f734 to 7f9bb39 Compare April 4, 2025 09:52
# Convert tools to OpenAI's expected type
openai_tools: List[ChatCompletionToolParam] = []
for tool in tools:
openai_tools.append(cast(ChatCompletionToolParam, tool))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can't we create the object with ChatCompletionToolParam(**tool) (or something similar)?

Copy link
Member Author

Choose a reason for hiding this comment

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

These are mypy related as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes I guess this is because openai_tools is declared to be List[ChatCompletionToolParam] but tool is a dict. That's why I thought instantiating the ChatCompletionToolParam could fix mypy without having to use cast, but I don't know the model exactly so maybe it doesn't work.

**params,
)

message = response.choices[0].message
Copy link
Contributor

Choose a reason for hiding this comment

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

Not for now because this is also the case in the already existing methods, but we should probably try to reduce code duplication between the sync and async calls.

@oskarhane oskarhane requested a review from stellasia April 4, 2025 13:43
@oskarhane oskarhane force-pushed the feature/llm-tool-invocation branch from 913c8be to b769242 Compare April 4, 2025 14:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants