-
Notifications
You must be signed in to change notification settings - Fork 914
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce id and model in ChatResponseMetadata
* Extend ChatResponseMetadata for Anthropic (blocking, streaming) * Add ChatResponseMetadata for Mistral AI (blocking) * Extend ChatResponseMetadata for OpenAI (blocking) * Deprecate gpt-4-vision-preview and replace its usage in tests because OpenAI rejects the calls (see: https://platform.openai.com/docs/deprecations) Fixes gh-936 Signed-off-by: Thomas Vitale <[email protected]>
- Loading branch information
1 parent
25a0372
commit 3227311
Showing
17 changed files
with
267 additions
and
31 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
62 changes: 62 additions & 0 deletions
62
...rc/main/java/org/springframework/ai/mistralai/metadata/MistralAiChatResponseMetadata.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,62 @@ | ||
package org.springframework.ai.mistralai.metadata; | ||
|
||
import org.springframework.ai.chat.metadata.ChatResponseMetadata; | ||
import org.springframework.ai.chat.metadata.EmptyUsage; | ||
import org.springframework.ai.chat.metadata.Usage; | ||
import org.springframework.ai.mistralai.api.MistralAiApi; | ||
import org.springframework.util.Assert; | ||
|
||
import java.util.HashMap; | ||
|
||
/** | ||
* {@link ChatResponseMetadata} implementation for {@literal Mistral AI}. | ||
* | ||
* @author Thomas Vitale | ||
* @see ChatResponseMetadata | ||
* @see Usage | ||
* @since 1.0.0 | ||
*/ | ||
public class MistralAiChatResponseMetadata extends HashMap<String, Object> implements ChatResponseMetadata { | ||
|
||
protected static final String AI_METADATA_STRING = "{ @type: %1$s, id: %2$s, model: %3$s, usage: %4$s }"; | ||
|
||
public static MistralAiChatResponseMetadata from(MistralAiApi.ChatCompletion result) { | ||
Assert.notNull(result, "Mistral AI ChatCompletion must not be null"); | ||
MistralAiUsage usage = MistralAiUsage.from(result.usage()); | ||
return new MistralAiChatResponseMetadata(result.id(), result.model(), usage); | ||
} | ||
|
||
private final String id; | ||
|
||
private final String model; | ||
|
||
private final Usage usage; | ||
|
||
protected MistralAiChatResponseMetadata(String id, String model, MistralAiUsage usage) { | ||
this.id = id; | ||
this.model = model; | ||
this.usage = usage; | ||
} | ||
|
||
@Override | ||
public String getId() { | ||
return this.id; | ||
} | ||
|
||
@Override | ||
public String getModel() { | ||
return this.model; | ||
} | ||
|
||
@Override | ||
public Usage getUsage() { | ||
Usage usage = this.usage; | ||
return usage != null ? usage : new EmptyUsage(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return AI_METADATA_STRING.formatted(getClass().getName(), getId(), getModel(), getUsage()); | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...ai-mistral-ai/src/main/java/org/springframework/ai/mistralai/metadata/MistralAiUsage.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,51 @@ | ||
package org.springframework.ai.mistralai.metadata; | ||
|
||
import org.springframework.ai.chat.metadata.Usage; | ||
import org.springframework.ai.mistralai.api.MistralAiApi; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* {@link Usage} implementation for {@literal Mistral AI}. | ||
* | ||
* @author Thomas Vitale | ||
* @since 1.0.0 | ||
* @see <a href="https://docs.mistral.ai/api/">Chat Completion API</a> | ||
*/ | ||
public class MistralAiUsage implements Usage { | ||
|
||
public static MistralAiUsage from(MistralAiApi.Usage usage) { | ||
return new MistralAiUsage(usage); | ||
} | ||
|
||
private final MistralAiApi.Usage usage; | ||
|
||
protected MistralAiUsage(MistralAiApi.Usage usage) { | ||
Assert.notNull(usage, "Mistral AI Usage must not be null"); | ||
this.usage = usage; | ||
} | ||
|
||
protected MistralAiApi.Usage getUsage() { | ||
return this.usage; | ||
} | ||
|
||
@Override | ||
public Long getPromptTokens() { | ||
return getUsage().promptTokens().longValue(); | ||
} | ||
|
||
@Override | ||
public Long getGenerationTokens() { | ||
return getUsage().completionTokens().longValue(); | ||
} | ||
|
||
@Override | ||
public Long getTotalTokens() { | ||
return getUsage().totalTokens().longValue(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return getUsage().toString(); | ||
} | ||
|
||
} |
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
Oops, something went wrong.