Skip to content

Improve tool usage with source generators#171

Merged
awaescher merged 29 commits intomainfrom
feature/ToolGenerator
Feb 21, 2025
Merged

Improve tool usage with source generators#171
awaescher merged 29 commits intomainfrom
feature/ToolGenerator

Conversation

@awaescher
Copy link
Copy Markdown
Owner

@awaescher awaescher commented Jan 15, 2025

Note

This is merged and can be used with version 5.1.1 or newer.

This pull request introduces a source generator that can generate Ollama tools from simple method implementations. Usage can be found in the ToolConsole demo:

/// <summary>
/// Gets the current weather for a given location.
/// </summary>
/// <param name="location">The location or city to get the weather for</param>
/// <param name="unit">The unit to measure the temperature in</param>
/// <returns>The weather for the given location</returns>
[OllamaTool]
public static string GetWeather(string location, Unit unit) => $"It's cold at only 6° {unit} in {location}.";
  • use object[] as tools instead of Tool[] in web requests to prevent dependencies
    see Metadata and return description for the tools #143
  • introduce a source generator that generates the code for tools (meta data and implementation)
    • allow static tool implementation as shown above for demos and simple calculations
    • allow non-static tool implementations with arguments and injected services
    • allow definition of tool meta data only (maybe with interfaces?)
    • make arguments required unless they are optional
    • automatic execution of requested tools by the tool calls provided by the LLM
    • backpropagation of the tool results (as message with role "Tool") to the LLM
    • use code enums for available values like "enum": ["celsius", "fahrenheit"] (see Unit in the code above)
      • fallback for enum values from the LLM that cannot get parsed (only for optional arguments)
    • parse meta data such as method and parameter names from XML summaries like in the code above
      • C#
      • F#?
      • Visual Basic?
    • include source generators in the OllamaSharp NuGet package
    • documentation → https://awaescher.github.io/OllamaSharp/docs/tool-support.html

You can find the generated code for the tools under the Dependencies node in your project:

image

@awaescher awaescher self-assigned this Jan 22, 2025
@awaescher awaescher marked this pull request as ready for review February 21, 2025 15:03
@awaescher awaescher merged commit 5bf9efc into main Feb 21, 2025
@awaescher awaescher deleted the feature/ToolGenerator branch February 21, 2025 15:03
awaescher added a commit that referenced this pull request Feb 21, 2025
Improve tool usage with source generators
awaescher added a commit that referenced this pull request Feb 24, 2025
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.

1 participant