OllamaHelper (VB.NET) Documentation
Overview
The VB.NET implementation of OllamaHelper provides core functionality for interacting with the Ollama API.
Installation
Basic Usage
Imports Helpers
' Initialize the helper
Dim helper = New OllamaHelper(
endpoint:="http://localhost:11434",
model:="llama2",
temperature:=0.7F,
maxTokens:=2000
)
' Get completion
Dim response = Await helper.GetCompletion("What is AI?")
' Stream response
Await For Each token In helper.GetCompletionStream("Tell me a story")
Console.Write(token)
Next
Core Features
Model Management
' List models
Dim models = Await helper.ListModels()
' Get model info
Dim info = Await helper.GetModelInfo("llama2")
' Set model parameters
helper.SetTemperature(0.8F)
helper.SetMaxTokens(1000)
Chat Functions
' Single message
Dim response = Await helper.Chat("Hello!")
' Conversation
Dim conversation = New List(Of ChatMessage) From {
New ChatMessage With {.Role = "user", .Content = "Hi"},
New ChatMessage With {.Role = "assistant", .Content = "Hello!"}
}
Dim reply = Await helper.ChatWithHistory(conversation)
Streaming
' Stream chat response
Await For Each token In helper.StreamChat("Tell me a joke")
Console.Write(token)
Next
' Stream with history
Await For Each token In helper.StreamChatWithHistory(conversation)
Console.Write(token)
Next
Error Handling
Try
Dim response = Await helper.GetCompletion(prompt)
Catch ex As OllamaException
Console.WriteLine($"Ollama error: {ex.Message}")
Catch ex As HttpRequestException
Console.WriteLine($"Network error: {ex.Message}")
End Try
Advanced Usage
Custom Configuration
Dim config = New OllamaConfig With {
.Temperature = 0.7F,
.MaxTokens = 2000,
.TopP = 0.9F,
.FrequencyPenalty = 0.1F,
.PresencePenalty = 0.1F
}
Dim helper = New OllamaHelper("http://localhost:11434", "llama2", config)
Batch Processing
Public Async Function ProcessBatch(prompts As IEnumerable(Of String)) As Task(Of IEnumerable(Of String))
Dim tasks = prompts.Select(Function(p) helper.GetCompletion(p))
Return Await Task.WhenAll(tasks)
End Function
Best Practices
- Connection Management
Public Class ManagedOllamaHelper
Implements IDisposable
Private ReadOnly _helper As OllamaHelper
Private _disposed As Boolean
Public Sub New()
_helper = New OllamaHelper()
End Sub
Public Sub Dispose() Implements IDisposable.Dispose
If Not _disposed Then
' Clean up resources
_disposed = True
End If
End Sub
End Class
- Rate Limiting
Public Class RateLimitedOllamaHelper
Private ReadOnly _helper As OllamaHelper
Private ReadOnly _semaphore As SemaphoreSlim
Public Sub New(Optional maxConcurrent As Integer = 5)
_helper = New OllamaHelper()
_semaphore = New SemaphoreSlim(maxConcurrent)
End Sub
Public Async Function GetCompletionWithRateLimit(prompt As String) As Task(Of String)
Await _semaphore.WaitAsync()
Try
Return Await _helper.GetCompletion(prompt)
Finally
_semaphore.Release()
End Try
End Function
End Class
- Retry Logic
Public Async Function GetCompletionWithRetry(prompt As String, Optional maxRetries As Integer = 3) As Task(Of String)
For i As Integer = 0 To maxRetries - 1
Try
Return Await _helper.GetCompletion(prompt)
Catch ex As OllamaException
If i = maxRetries - 1 Then Throw
Await Task.Delay(1000 * (i + 1))
End Try
Next
Throw New Exception("Max retries exceeded")
End Function
Performance Optimization
- Caching
Public Class CachedOllamaHelper
Private ReadOnly _helper As OllamaHelper
Private ReadOnly _cache As MemoryCache
Public Sub New()
_helper = New OllamaHelper()
_cache = New MemoryCache(New MemoryCacheOptions())
End Sub
Public Async Function GetCachedCompletion(prompt As String) As Task(Of String)
Dim key = ComputeHash(prompt)
Dim cached As String = Nothing
If _cache.TryGetValue(key, cached) Then
Return cached
End If
Dim response = Await _helper.GetCompletion(prompt)
_cache.Set(key, response, TimeSpan.FromHours(1))
Return response
End Function
End Class
- Connection Pooling
Public Class PooledOllamaHelper
Private ReadOnly _pool As ConcurrentBag(Of OllamaHelper)
Private ReadOnly _poolSize As Integer
Public Sub New(Optional poolSize As Integer = 5)
_poolSize = poolSize
_pool = New ConcurrentBag(Of OllamaHelper)()
For i As Integer = 0 To poolSize - 1
_pool.Add(New OllamaHelper())
Next
End Sub
Public Async Function GetCompletion(prompt As String) As Task(Of String)
Dim helper As OllamaHelper = Nothing
If Not _pool.TryTake(helper) Then
helper = New OllamaHelper()
End If
Try
Return Await helper.GetCompletion(prompt)
Finally
_pool.Add(helper)
End Try
End Function
End Class
Testing
Public Class OllamaHelperTests
Private _helper As OllamaHelper
Private _mockFactory As Mock(Of IHttpClientFactory)
Public Sub Setup()
_mockFactory = New Mock(Of IHttpClientFactory)()
_helper = New OllamaHelper(
"http://localhost:11434",
"llama2",
httpClientFactory:=_mockFactory.Object
)
End Sub
Public Async Function GetCompletion_ValidPrompt_ReturnsResponse() As Task
' Arrange
Dim prompt = "Test prompt"
' Act
Dim response = Await _helper.GetCompletion(prompt)
' Assert
Assert.IsNotNull(response)
End Function
End Class
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.
