Git Product home page Git Product logo

ollama's Introduction

Ollama SDK for .NET 🦙

Nuget package dotnet License: MIT Discord

Features 🔥

  • Fully generated C# SDK based on OpenAPI specification using OpenApiGenerator
  • Automatic releases of new preview versions if there was an update to the OpenAPI specification
  • Source generator to define tools natively through C# interfaces
  • All modern .NET features - nullability, trimming, NativeAOT, etc.
  • Support .Net Framework/.Net Standard 2.0
  • Support for all Ollama API endpoints including chats, embeddings, listing models, pulling and creating new models, and more.

Usage

Initializing

using var ollama = new OllamaApiClient();

var models = await ollama.Models.ListModelsAsync();

// Pulling a model and reporting progress
await foreach (var response in ollama.PullModelAsync("all-minilm", stream: true))
{
    Console.WriteLine($"{response.Status}. Progress: {response.Completed}/{response.Total}");
}
// or just pull the model and wait for it to finish
await ollama.Models.PullModelAndEnsureSuccessAsync("all-minilm");

// Generating an embedding
var embedding = await ollama.Embeddings.GenerateEmbeddingAsync(
    model: "all-minilm",
    prompt: "hello");

// Streaming a completion directly into the console
// keep reusing the context to keep the chat topic going
IList<long>? context = null;
var enumerable = ollama.Completions.GenerateCompletionAsync("llama3", "answer 5 random words", stream: true);
await foreach (var response in enumerable)
{
    Console.WriteLine($"> {response.Response}");
    
    context = response.Context;
}

var lastResponse = await ollama.Completions.GenerateCompletionAsync("llama3", "answer 123", stream: false, context: context).WaitAsync();
Console.WriteLine(lastResponse.Response);

var chat = ollama.Chat("mistral");
while (true)
{
    var message = await chat.SendAsync("answer 123");
    
    Console.WriteLine(message.Content);
    
    var newMessage = Console.ReadLine();
    await chat.Send(newMessage);
}

Tools

using var ollama = new OllamaApiClient();
var chat = ollama.Chat(
    model: "llama3.1",
    systemMessage: "You are a helpful weather assistant.",
    autoCallTools: true);

var service = new WeatherService();
chat.AddToolService(service.AsTools(), service.AsCalls());

try
{
    _ = await chat.SendAsync("What is the current temperature in Dubai, UAE in Celsius?");
}
finally
{
    Console.WriteLine(chat.PrintMessages());
}
> System:
You are a helpful weather assistant.
> User:
What is the current temperature in Dubai, UAE in Celsius?
> Assistant:
Tool calls:
GetCurrentWeather({"location":"Dubai, UAE","unit":"celsius"})
> Tool:
{"location":"Dubai, UAE","temperature":22,"unit":"celsius","description":"Sunny"}
> Assistant:
The current temperature in Dubai, UAE is 22°C.
public enum Unit
{
    Celsius,
    Fahrenheit,
}

public class Weather
{
    public string Location { get; set; } = string.Empty;
    public double Temperature { get; set; }
    public Unit Unit { get; set; }
    public string Description { get; set; } = string.Empty;
}

[OllamaTools]
public interface IWeatherFunctions
{
    [Description("Get the current weather in a given location")]
    public Task<Weather> GetCurrentWeatherAsync(
        [Description("The city and state, e.g. San Francisco, CA")] string location,
        Unit unit = Unit.Celsius,
        CancellationToken cancellationToken = default);
}

public class WeatherService : IWeatherFunctions
{
    public Task<Weather> GetCurrentWeatherAsync(string location, Unit unit = Unit.Celsius, CancellationToken cancellationToken = default)
    {
        return Task.FromResult(new Weather
        {
            Location = location,
            Temperature = 22.0,
            Unit = unit,
            Description = "Sunny",
        });
    }
}

Credits

Icon and name were reused from the amazing Ollama project.
The project was forked from this repository, after which automatic code generation was applied based on this OpenAPI specification (in the future it will be replaced by the official one, if one appears)

Support

Priority place for bugs: https://github.com/tryAGI/Ollama/issues
Priority place for ideas and general questions: https://github.com/tryAGI/Ollama/discussions
Discord: https://discord.gg/Ca2xhfBf3v

ollama's People

Contributors

havendv avatar dependabot[bot] avatar github-actions[bot] avatar

Stargazers

Auguste @ kiss-code.com avatar  avatar Vitaliy avatar  avatar David Dennison avatar  avatar Carlos Fonseca avatar HuyHo avatar BlueSharkPartners avatar  avatar wake0up0ne0 avatar Victor Tomaili avatar JD Davis avatar 虫子樱桃 avatar  avatar  avatar Sergio Luiz avatar

Watchers

 avatar kobaeugenea avatar

ollama's Issues

how to Generate openapi code?

Describe the bug

/main/src/libs/Ollama/Generated/net8.0/OpenApiGenerator

Steps to reproduce the bug

1

Expected behavior

1

Screenshots

1

NuGet package version

1

Additional context

1

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.