FluentAI.ChatCompletions.Abstraction 1.0.0-beta.3

This is a prerelease version of FluentAI.ChatCompletions.Abstraction.
There is a newer version of this package available.
See the version list below for details.
dotnet add package FluentAI.ChatCompletions.Abstraction --version 1.0.0-beta.3                
NuGet\Install-Package FluentAI.ChatCompletions.Abstraction -Version 1.0.0-beta.3                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FluentAI.ChatCompletions.Abstraction" Version="1.0.0-beta.3" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FluentAI.ChatCompletions.Abstraction --version 1.0.0-beta.3                
#r "nuget: FluentAI.ChatCompletions.Abstraction, 1.0.0-beta.3"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FluentAI.ChatCompletions.Abstraction as a Cake Addin
#addin nuget:?package=FluentAI.ChatCompletions.Abstraction&version=1.0.0-beta.3&prerelease

// Install FluentAI.ChatCompletions.Abstraction as a Cake Tool
#tool nuget:?package=FluentAI.ChatCompletions.Abstraction&version=1.0.0-beta.3&prerelease                

Fluent AI

NuGet version (FluentAI.ChatCompletions.Abstraction)

Note: This is the abstraction package. The implementation can be found in the FluentAI.ChatCompletions.OpenAI package and others.

Fluent AI is a powerful library designed to build and execute advanced prompts using OpenAI's various ChatGPT models. It leverages the capabilities of Azure.AI.OpenAI and NJsonSchema packages to provide a seamless experience for integrating AI into your .NET applications.

Features

  • Fluent Building of User and System Prompts: Easily construct prompts for ChatGPT models.
  • Custom Chat Tools Integration: Integrate custom chat functions to enable ChatGPT to call arbitrary .NET methods and utilize returned values.
  • Structured Responses: Automatically instruct the model to respond in a specified JSON format.
  • Built-in Validation: Validate ChatGPT responses using JSON Schema and auto-retry in case the model returns incorrectly formatted answers.

Example Usage

var openAiToken = "...";

var request = new ChatCompletionOpenAiClient(openAiToken)
    .ToCompletionsBuilder()
    .UseChatGpt4o() // Specify model
    .UseChatTool(new FetchUrlTool()) // Add custom .NET methods that the model can call
    .UserPrompt("Give me a short description of the following webpage: https://example.com")
    .UseResponseSchema<ChatGptResponse>() // Auto-generate JSON schema for the model and instruct it to use it
    .BuildCompletionsRequest();

ChatGptResponse response = await request.GetStructuredResponse<ChatGptResponse>(); // Get structured and validated response from ChatGPT

Console.WriteLine(response.Text);

// Instruct the model to use this type as the answer
[Description("This is the response model you should use to send answers to questions")]
public class ChatGptResponse
{
    // Specify descriptions of properties and add additional validation like Url, Phone, Email, Date, etc.
    [Description("Your response message"), Required]
    public string Text { get; set; }
}

Implementation of FetchUrlTool

/// <summary>
/// Handles requests to fetch content from a specified URL.
/// </summary>
[Description("Gets content by specified URL")]
public class FetchUrlTool : ChatCompletionToolBase<FetchUrlTool.FetchUrlToolRequest, FetchUrlTool.FetchUrlToolResponse>
{
    public FetchUrlTool() : base("fetch_url") {}

    /// <summary>
    /// Handles the specified request to fetch content from a URL.
    /// </summary>
    /// <param name="request">The request containing the URL to fetch.</param>
    /// <returns>A task that represents the asynchronous operation. The task result contains the response with the fetched content.</returns>
    public override async Task<FetchUrlToolResponse> Handle(FetchUrlToolRequest request)
    {
        var httpClient = new HttpClient();
        var response = await httpClient.GetStringAsync(request.Url);
        return new FetchUrlToolResponse() { Content = response };
    }

    /// <summary>
    /// Represents a request to fetch content from a specified URL.
    /// </summary>
    public class FetchUrlToolRequest
    {
        /// <summary>
        /// Gets or sets the URL to fetch.
        /// </summary>
        [Description("The URL"), Required, Url]
        public string Url { get; set; }
    }

    /// <summary>
    /// Represents the response containing the content fetched from a URL.
    /// </summary>
    public class FetchUrlToolResponse
    {
        /// <summary>
        /// Gets or sets the content fetched from the URL.
        /// </summary>
        public string Content { get; set; }
    }
}

Important Note

The DescriptionAttribute is mandatory as it describes the meaning of the properties to the model, ensuring that the AI understands the context and purpose of each property.

Contributing

Contributions are welcome! Please feel free to submit a pull request or open an issue.

License

This project is licensed under the MIT License. See the LICENSE file for more details.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on FluentAI.ChatCompletions.Abstraction:

Package Downloads
FluentAI.ChatCompletions.OpenAI

The package for constructing and executing chat completions using the OpenAI API. This package allows you to easily configure and execute chat completions using different OpenAI models, add system and user prompts, and define structured response schemas. Additionally, the package allows you to create and use chat tools, enabling ChatGPT to call .NET code during execution.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.0 128 7/4/2024
1.0.0-beta.3 56 6/18/2024

Initial release