OllamaHelper (C#) Documentation
Overview
The C# implementation of OllamaHelper provides core functionality for interacting with the Ollama API.
Installation
Basic Usage
using Helpers;
// Initialize the helper
var helper = new OllamaHelper(
endpoint: "http://localhost:11434",
model: "llama2",
temperature: 0.7f,
maxTokens: 2000
);
// Get completion
var response = await helper.GetCompletion("What is AI?");
// Stream response
await foreach (var token in helper.GetCompletionStream("Tell me a story"))
{
Console.Write(token);
}
Core Features
Model Management
// List models
var models = await helper.ListModels();
// Get model info
var info = await helper.GetModelInfo("llama2");
// Set model parameters
helper.SetTemperature(0.8f);
helper.SetMaxTokens(1000);
Chat Functions
// Single message
var response = await helper.Chat("Hello!");
// Conversation
var conversation = new List
{
new ChatMessage { Role = "user", Content = "Hi" },
new ChatMessage { Role = "assistant", Content = "Hello!" }
};
var reply = await helper.ChatWithHistory(conversation);
Streaming
// Stream chat response
await foreach (var token in helper.StreamChat("Tell me a joke"))
{
Console.Write(token);
}
// Stream with history
await foreach (var token in helper.StreamChatWithHistory(conversation))
{
Console.Write(token);
}
Error Handling
try
{
var response = await helper.GetCompletion(prompt);
}
catch (OllamaException ex)
{
Console.WriteLine($"Ollama error: {ex.Message}");
}
catch (HttpRequestException ex)
{
Console.WriteLine($"Network error: {ex.Message}");
}
Advanced Usage
Custom Configuration
var config = new OllamaConfig
{
Temperature = 0.7f,
MaxTokens = 2000,
TopP = 0.9f,
FrequencyPenalty = 0.1f,
PresencePenalty = 0.1f
};
var helper = new OllamaHelper("http://localhost:11434", "llama2", config);
Batch Processing
public async Task> ProcessBatch(IEnumerable prompts)
{
var tasks = prompts.Select(p => helper.GetCompletion(p));
return await Task.WhenAll(tasks);
}
Best Practices
- Connection Management
public class ManagedOllamaHelper : IDisposable
{
private readonly OllamaHelper _helper;
private bool _disposed;
public ManagedOllamaHelper()
{
_helper = new OllamaHelper();
}
public void Dispose()
{
if (!_disposed)
{
// Clean up resources
_disposed = true;
}
}
}
- Rate Limiting
public class RateLimitedOllamaHelper
{
private readonly OllamaHelper _helper;
private readonly SemaphoreSlim _semaphore;
public RateLimitedOllamaHelper(int maxConcurrent = 5)
{
_helper = new OllamaHelper();
_semaphore = new SemaphoreSlim(maxConcurrent);
}
public async Task GetCompletionWithRateLimit(string prompt)
{
await _semaphore.WaitAsync();
try
{
return await _helper.GetCompletion(prompt);
}
finally
{
_semaphore.Release();
}
}
}
- Retry Logic
public async Task GetCompletionWithRetry(string prompt, int maxRetries = 3)
{
for (int i = 0; i < maxRetries; i++)
{
try
{
return await _helper.GetCompletion(prompt);
}
catch (OllamaException ex)
{
if (i == maxRetries - 1) throw;
await Task.Delay(1000 * (i + 1));
}
}
throw new Exception("Max retries exceeded");
}
Performance Optimization
- Caching
public class CachedOllamaHelper
{
private readonly OllamaHelper _helper;
private readonly MemoryCache _cache;
public CachedOllamaHelper()
{
_helper = new OllamaHelper();
_cache = new MemoryCache(new MemoryCacheOptions());
}
public async Task GetCachedCompletion(string prompt)
{
var key = ComputeHash(prompt);
if (_cache.TryGetValue(key, out string cached))
return cached;
var response = await _helper.GetCompletion(prompt);
_cache.Set(key, response, TimeSpan.FromHours(1));
return response;
}
}
- Connection Pooling
public class PooledOllamaHelper
{
private readonly ConcurrentBag _pool;
private readonly int _poolSize;
public PooledOllamaHelper(int poolSize = 5)
{
_poolSize = poolSize;
_pool = new ConcurrentBag();
for (int i = 0; i < poolSize; i++)
{
_pool.Add(new OllamaHelper());
}
}
public async Task GetCompletion(string prompt)
{
OllamaHelper helper;
if (!_pool.TryTake(out helper))
{
helper = new OllamaHelper();
}
try
{
return await helper.GetCompletion(prompt);
}
finally
{
_pool.Add(helper);
}
}
}
Testing
[TestClass]
public class OllamaHelperTests
{
private OllamaHelper _helper;
private Mock _mockFactory;
[TestInitialize]
public void Setup()
{
_mockFactory = new Mock();
_helper = new OllamaHelper(
"http://localhost:11434",
"llama2",
httpClientFactory: _mockFactory.Object
);
}
[TestMethod]
public async Task GetCompletion_ValidPrompt_ReturnsResponse()
{
// Arrange
var prompt = "Test prompt";
// Act
var response = await _helper.GetCompletion(prompt);
// Assert
Assert.IsNotNull(response);
}
}
Legal Disclaimer
This documentation and associated helper scripts are provided "as is" without warranty of any kind, either express or implied.
- The code examples and helper functions are for illustrative purposes only.
- Users should thoroughly test any implementation in their specific environment.
- The authors are not responsible for any issues or damages arising from the use of these scripts.
- Always follow security best practices and your organization's coding guidelines.
