Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(py): Added collecting of usage info for Ollama API #2361

Merged
merged 2 commits into from
Mar 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 40 additions & 6 deletions py/plugins/ollama/src/genkit/plugins/ollama/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,17 @@
import mimetypes
from typing import Literal

import ollama as ollama_api
from pydantic import BaseModel, Field, HttpUrl

import ollama as ollama_api
from genkit.ai.model import get_basic_usage_stats
from genkit.core.action import ActionRunContext
from genkit.core.typing import (
GenerateRequest,
GenerateResponse,
GenerateResponseChunk,
GenerationCommonConfig,
GenerationUsage,
Media,
MediaPart,
Message,
Expand Down Expand Up @@ -61,12 +63,12 @@ async def generate(
content = [TextPart(text='Failed to get response from Ollama API')]

if self.model_definition.api_type == OllamaAPITypes.CHAT:
chat_response = await self._chat_with_ollama(
api_response = await self._chat_with_ollama(
request=request, ctx=ctx
)
if chat_response:
if api_response:
content = self._build_multimodal_chat_response(
chat_response=chat_response,
chat_response=api_response,
)
elif self.model_definition.api_type == OllamaAPITypes.GENERATE:
api_response = await self._generate_ollama_response(
Expand All @@ -75,16 +77,32 @@ async def generate(
if api_response:
content = [TextPart(text=api_response.response)]
else:
LOG.error(f'Unresolved API type: {self.model_definition.api_type}')
raise ValueError(
f'Unresolved API type: {self.model_definition.api_type}'
)

if self.is_streaming_request(ctx=ctx):
content = []

response_message = Message(
role=Role.MODEL,
content=content,
)

basic_generation_usage = get_basic_usage_stats(
input_=request.messages,
response=response_message,
)

return GenerateResponse(
message=Message(
role=Role.MODEL,
content=content,
)
),
usage=self.get_usage_info(
basic_generation_usage=basic_generation_usage,
api_response=api_response,
),
)

async def _chat_with_ollama(
Expand Down Expand Up @@ -277,3 +295,19 @@ def _to_ollama_role(
@staticmethod
def is_streaming_request(ctx: ActionRunContext | None) -> bool:
return ctx and ctx.is_streaming

@staticmethod
def get_usage_info(
basic_generation_usage: GenerationUsage,
api_response: ollama_api.GenerateResponse | ollama_api.ChatResponse,
) -> GenerationUsage:
if api_response:
basic_generation_usage.input_tokens = (
api_response.prompt_eval_count or 0
)
basic_generation_usage.output_tokens = api_response.eval_count or 0
basic_generation_usage.total_tokens = (
basic_generation_usage.input_tokens
+ basic_generation_usage.output_tokens
)
return basic_generation_usage
Loading