langchainrb icon indicating copy to clipboard operation
langchainrb copied to clipboard

Create a Generator that would generate an Open API spec from a passed in Ruby class (Improve Tool creation for Assistants)

Open andreibondarev opened this issue 1 year ago • 7 comments

Description

We'd like to improve the experience of creating Tools (classes that inherit from Langchain::Tool::Base) for Assistants to use. Open API spec is difficult to compose and we'd like to see if an LLM itself can generate the JSON spec given a Ruby class.

Tasks

  • Create a .yaml prompt that instructs an LLM to generate a JSON Open API spec (example: database.json) and accepts a Ruby class.
  • Create a Generator class (under /utils? or maybe new directory /generators?) where this functionality will live.
  • This Generator class will output a .json file with the generator Open API spec.

andreibondarev avatar Mar 25 '24 14:03 andreibondarev

Google added an enable_automatic_function_calling option to their python library that doesn't seem to requre a spec definition. Maybe do something like this: https://github.com/google-gemini/cookbook/blob/main/examples/Agents_Function_Calling_Barista_Bot.ipynb

They say:

In the Python SDK you can pass functions directly to the model constructor, where the SDK will inspect the type signatures and docstrings to define the tools. For this reason it's important that you correctly type each of the parameters, give the functions sensible names and detailed docstrings.

mattlindsey avatar Apr 24 '24 22:04 mattlindsey

@mattlindsey Nice find! Unfortunately, unlike Python, Ruby does not have built-in type annotations.

andreibondarev avatar Apr 25 '24 01:04 andreibondarev

@mattlindsey I attached a branch where I'm working on an approach: generate-method-annotations

andreibondarev avatar Apr 25 '24 13:04 andreibondarev

@andreibondarev Looks great. I can test it for you when ready. A further enhancement could be to make the annotation files optional by looking at the YARD annotations, or for tools that use RBS gem annotations.

mattlindsey avatar Apr 25 '24 14:04 mattlindsey

I'm a little bit confused how should I define tool class for an assistant. I watched the ecommerce assistant presentation: https://www.loom.com/share/83aa4fd8dccb492aad4ca95da40ed0b2?sid=12b11c98-c6b7-4d98-8be8-1f35a2b3861a. However, the linked repository changed a lot. There is no JSON function definition any more. Is the repository state connected with changes introduced by this thread?

Olgagr avatar Jul 31 '24 06:07 Olgagr

I'm a little bit confused how should I define tool class for an assistant. I watched the ecommerce assistant presentation: https://www.loom.com/share/83aa4fd8dccb492aad4ca95da40ed0b2?sid=12b11c98-c6b7-4d98-8be8-1f35a2b3861a. However, the linked repository changed a lot. There is no JSON function definition any more. Is the repository state connected with changes introduced by this thread?

Hi @Olgagr, here's the short answer:

  • Your Tool class does not need to inherit from Langchain::Tool::Base anymore.
  • Your Tool class needs to be extended with extend Langchain::ToolDefinition.
  • Your Tool class needs to declare all methods you'd like to expose using the define_function ... methods. See example: https://github.com/patterns-ai-core/langchainrb/blob/main/lib/langchain/tool/news_retriever.rb#L14-L40

I'll try recording an updated video today to showcase the new syntax. Btw -- I updated the demo with the gem updates.

andreibondarev avatar Jul 31 '24 12:07 andreibondarev

Awesome! Thank you @andreibondarev !

Olgagr avatar Jul 31 '24 14:07 Olgagr

This is no longer needed since we got rid of the static .json files and @dghirardo built a beautiful interface here: https://github.com/patterns-ai-core/langchainrb/pull/708

andreibondarev avatar Sep 29 '24 16:09 andreibondarev