AIAgentSharp.OpenAI 1.0.11

dotnet add package AIAgentSharp.OpenAI --version 1.0.11
                    
NuGet\Install-Package AIAgentSharp.OpenAI -Version 1.0.11
                    
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="AIAgentSharp.OpenAI" Version="1.0.11" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="AIAgentSharp.OpenAI" Version="1.0.11" />
                    
Directory.Packages.props
<PackageReference Include="AIAgentSharp.OpenAI" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add AIAgentSharp.OpenAI --version 1.0.11
                    
#r "nuget: AIAgentSharp.OpenAI, 1.0.11"
                    
#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.
#:package AIAgentSharp.OpenAI@1.0.11
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=AIAgentSharp.OpenAI&version=1.0.11
                    
Install as a Cake Addin
#tool nuget:?package=AIAgentSharp.OpenAI&version=1.0.11
                    
Install as a Cake Tool

AIAgentSharp.OpenAI

OpenAI integration for AIAgentSharp - LLM-powered agents with OpenAI models.

Features

  • OpenAI API Integration: Full support for OpenAI's chat completion API
  • Function Calling: Native support for OpenAI function calling
  • Configurable Models: Support for GPT-4o, GPT-4o-mini, GPT-3.5-turbo, and more
  • Advanced Configuration: Comprehensive settings for temperature, tokens, penalties, and more
  • Enterprise Support: Organization ID and custom endpoint support
  • Error Handling: Robust error handling with retry logic
  • Logging: Comprehensive logging for debugging and monitoring

Installation

dotnet add package AIAgentSharp.OpenAI

Quick Start

Basic Usage

using AIAgentSharp;
using AIAgentSharp.Agents;
using AIAgentSharp.OpenAI;
using AIAgentSharp.StateStores;

// Create OpenAI client
var llm = new OpenAiLlmClient("your-openai-api-key");

// Create agent
var store = new MemoryAgentStateStore();
var agent = new Agent(llm, store);

// Run agent
var result = await agent.RunAsync("my-agent", "Hello, how are you?", new List<ITool>());
Console.WriteLine(result.FinalOutput);

Advanced Configuration

// Create optimized configuration for agent reasoning
var config = OpenAiConfiguration.CreateForAgentReasoning();

// Or create custom configuration
var customConfig = new OpenAiConfiguration
{
    Model = "gpt-4o",
    Temperature = 0.2f,
    MaxTokens = 6000,
    EnableFunctionCalling = true,
    MaxRetries = 5,
    RequestTimeout = TimeSpan.FromMinutes(3)
};

// Create client with configuration
var llm = new OpenAiLlmClient("your-openai-api-key", customConfig);

Enterprise Usage

var config = new OpenAiConfiguration
{
    Model = "gpt-4o",
    OrganizationId = "org-your-org-id",
    ApiBaseUrl = "https://your-custom-endpoint.com/v1",
    EnableFunctionCalling = true
};

var llm = new OpenAiLlmClient("your-api-key", config);

Configuration Options

Model Selection

// Cost-effective reasoning
var config = OpenAiConfiguration.CreateForCostEfficiency(); // Uses gpt-3.5-turbo

// Balanced performance
var config = OpenAiConfiguration.CreateForAgentReasoning(); // Uses gpt-4o-mini

// Maximum capability
var config = OpenAiConfiguration.CreateForCreativeTasks(); // Uses gpt-4o

Temperature Settings

var config = new OpenAiConfiguration
{
    Temperature = 0.0f,  // Most deterministic
    // Temperature = 0.1f,  // Good for reasoning (default)
    // Temperature = 0.7f,  // Balanced creativity
    // Temperature = 1.0f,  // More creative
};

Token Management

var config = new OpenAiConfiguration
{
    MaxTokens = 2000,    // Shorter responses, lower cost
    // MaxTokens = 4000,  // Default
    // MaxTokens = 8000,  // Longer responses, higher cost
};

Function Calling

The OpenAI client supports native function calling:

// Create tools
var tools = new List<ITool>
{
    new WeatherTool(),
    new CalculatorTool()
};

// Function calling is automatically enabled when tools are provided
var result = await agent.RunAsync("agent-id", "What's 2+2 and the weather in NYC?", tools);

Error Handling

The client includes robust error handling:

try
{
    var result = await agent.RunAsync("agent-id", "Hello", tools);
}
catch (InvalidOperationException ex)
{
    Console.WriteLine($"OpenAI API error: {ex.Message}");
}
catch (OperationCanceledException)
{
    Console.WriteLine("Request was cancelled or timed out");
}

Logging

Enable detailed logging for debugging:

var logger = new ConsoleLogger();
var llm = new OpenAiLlmClient("your-api-key", logger: logger);

// Or use your own logger implementation
var customLogger = new MyCustomLogger();
var llm = new OpenAiLlmClient("your-api-key", logger: customLogger);

Performance Optimization

Cost Optimization

var config = OpenAiConfiguration.CreateForCostEfficiency();
// - Uses gpt-3.5-turbo (cheaper)
// - Lower max tokens
// - Fewer retries
// - Shorter timeout

Speed Optimization

var config = new OpenAiConfiguration
{
    Model = "gpt-4o-mini",  // Fastest model
    MaxTokens = 2000,       // Shorter responses
    RequestTimeout = TimeSpan.FromMinutes(1),  // Shorter timeout
    MaxRetries = 2          // Fewer retries
};

Quality Optimization

var config = new OpenAiConfiguration
{
    Model = "gpt-4o",       // Highest quality
    Temperature = 0.0f,     // Most deterministic
    MaxTokens = 8000,       // Longer responses
    RequestTimeout = TimeSpan.FromMinutes(5)  // Longer timeout
};

Supported Models

  • gpt-4o: Most capable model, best for complex reasoning
  • gpt-4o-mini: Fast and cost-effective, good for most tasks
  • gpt-4-turbo: Good balance of capability and cost
  • gpt-3.5-turbo: Legacy model, still effective for simple tasks

Dependencies

  • AIAgentSharp: Core agent framework
  • OpenAI: Official OpenAI .NET SDK

License

This package is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET 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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.11 103 8/22/2025
1.0.10 97 8/22/2025
1.0.9 124 8/20/2025
1.0.8 114 8/18/2025
1.0.7 118 8/18/2025
1.0.6 121 8/18/2025
1.0.5 121 8/18/2025
1.0.0 123 8/18/2025