Skip to content

thuonglbl/OllamaWithExtensionAndFunctionCalling

Repository files navigation

OllamaWithExtensionAndFunctionCalling

Overview

This project is an ASP.NET Core web API that integrates with Ollama for AI-powered chat, including function calling capabilities. The API allows the AI model to call custom C# functions (such as getting weather or location) and supports OpenAPI documentation and output caching for better performance.

Features

  • AI Chat Integration: Connects to an Ollama server and uses a configurable chat model.
  • Function Calling: The AI can invoke C# functions to provide dynamic responses.
  • OpenAPI Support: Automatically generates API documentation.
  • Output Caching: Improves performance for frequent queries.

Requirements

  • .NET 9.0 SDK installed
  • Ollama installed and running locally (default: http://localhost:11434)
  • At least one chat model pulled to Ollama (e.g., llama3.2)

Getting Started

1. Clone the repository

git clone cd OllamaWithExtensionAndFunctionCalling

2. Configure appsettings.json

Edit appsettings.json to match your Ollama setup:

3. Pull a model in Ollama

ollama pull llama3.2

4. Run the application

dotnet run

The API will be available at http://localhost:5000 (or another port if configured).

API Endpoints

POST /chat

Send a message to the AI and receive a response. The AI can call built-in functions for weather and location.

Request body:

"Hello, what's the weather in my location?"

Response:

{ "response": "It's sunny in Vienna, Austria" }

GET /

Redirects to the Scalar API reference.

OpenAPI Documentation

  • OpenAPI/Swagger: /openapi.json
  • Scalar API reference: /scalar/v1

Custom Functions

The AI can call these C# functions:

  • GetWeather: Returns a random weather status.
  • GetLocation: Returns a fixed location ("Vienna, Austria").
  • GetWeatherAndLocation: Combines weather and location.

Performance Optimization

Output caching is enabled for frequent queries, with a default expiration of 5 minutes.

License

This project is for educational and demonstration purposes.


Feel free to modify this README to fit your specific needs!

About

Extend local LLMs with function calling. A practical C# implementation for Ollama integration.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages