Skip to content

Add token tracking, API key auth, and tool calling to Ollama client - #8

Merged
ExtCan merged 4 commits into
mainfrom
copilot/improve-ollama-implementation
Dec 27, 2025
Merged

ExtCan merged 4 commits into
mainfrom
copilot/improve-ollama-implementation

Conversation

Copilot AI commented Dec 27, 2025 •

Copy link
Copy Markdown
Contributor

Enhances Ollama integration with production-ready features: full token usage metrics, optional API key authentication, and tool/function calling for web search and URL reading.

Token Usage Tracking

  • Captures prompt_eval_count and eval_count from API responses
  • Exposes per-request and cumulative session metrics via LastPromptTokens, TotalTokensUsed, etc.
  • Handles multi-request flows (tool calls) correctly by accumulating tokens across both initial and follow-up requests
var response = await client.ChatAsync("What is 2+2?");
Console.WriteLine(client.GetTokenUsageSummary());
// Output: Last Request: 45 prompt + 12 completion = 57 total
//         Total Session: 45 prompt + 12 completion = 57 total

API Key Authentication

  • ApiKey property setter automatically updates Authorization: Bearer header
  • Persisted in AppSettings with password-masked UI input
  • Empty key removes authentication header

Web Search & URL Reading

  • Implements Ollama's tool/function calling API for DuckDuckGo search and URL content fetching
  • Tools dynamically filtered by enabled features to minimize request payload
  • Two-phase flow: model requests tool → execute locally → inject results → final response
  • URL content: HTML stripped, decoded, truncated to 2000 chars (MAX_URL_CONTENT_LENGTH)
client.EnableWebSearch = true;
client.EnableUrlReading = true;
var response = await client.ChatAsync("What's on example.com?");
// Model can call read_url tool, response incorporates webpage content

UI controls added to Settings > Ollama tab for all features. Backward compatible—all features opt-in.

Original prompt

Make the Ollama implementation better

  • full token usage
  • able to use API key
  • Websearching and URL reading

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits December 27, 2025 01:03
… capabilities to Ollama

Co-authored-by: ExtCan <60326708+ExtCan@users.noreply.github.com>
Co-authored-by: ExtCan <60326708+ExtCan@users.noreply.github.com>
…onstant for URL length

Co-authored-by: ExtCan <60326708+ExtCan@users.noreply.github.com>
Copilot AI changed the title [WIP] Enhance Ollama implementation with full token usage and API key support Add token tracking, API key auth, and tool calling to Ollama client Dec 27, 2025
Copilot AI requested a review from ExtCan December 27, 2025 01:11
@ExtCan
ExtCan marked this pull request as ready for review December 27, 2025 01:19
@ExtCan
ExtCan merged commit bdbf01c into main Dec 27, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants