-
Notifications
You must be signed in to change notification settings - Fork 843
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Ollama usage metadata to add embedding support
- Extend the OllamaApi.EmbeddingsResponse with total_duration, load_duration and prompt_eval_count fields - Rename OllamaUsage to OllamaChatUsage for clarity - Add OllamaEmbeddingUsage to track embedding-specific usage metrics - Update OllamaEmbeddingModel to use OllamaEmbeddingUsage - Extend EmbeddingsResponse with additional metadata fields - Update tests to reflect new usage tracking for embeddings Resolves #1536
- Loading branch information
Showing
8 changed files
with
96 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
...-ai-ollama/src/main/java/org/springframework/ai/ollama/metadata/OllamaEmbeddingUsage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
/* | ||
* Copyright 2023 - 2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.springframework.ai.ollama.metadata; | ||
|
||
import java.util.Optional; | ||
|
||
import org.springframework.ai.chat.metadata.Usage; | ||
import org.springframework.ai.ollama.api.OllamaApi.EmbeddingsResponse; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* {@link Usage} implementation for {@literal Ollama} embeddings. | ||
* | ||
* @see Usage | ||
* @author Christian Tzolov | ||
*/ | ||
public class OllamaEmbeddingUsage implements Usage { | ||
|
||
protected static final String AI_USAGE_STRING = "{ promptTokens: %1$d, generationTokens: %2$d, totalTokens: %3$d }"; | ||
|
||
public static OllamaEmbeddingUsage from(EmbeddingsResponse response) { | ||
Assert.notNull(response, "OllamaApi.EmbeddingsResponse must not be null"); | ||
return new OllamaEmbeddingUsage(response); | ||
} | ||
|
||
private Long promptTokens; | ||
|
||
public OllamaEmbeddingUsage(EmbeddingsResponse response) { | ||
this.promptTokens = Optional.ofNullable(response.promptEvalCount()).map(Integer::longValue).orElse(0L); | ||
} | ||
|
||
@Override | ||
public Long getPromptTokens() { | ||
return this.promptTokens; | ||
} | ||
|
||
@Override | ||
public Long getGenerationTokens() { | ||
return 0L; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return AI_USAGE_STRING.formatted(getPromptTokens(), getGenerationTokens(), getTotalTokens()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters