You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
First of all, my goal is to implement chatbot with billing based on the token usage as the original API does.
It's known that OpenAI's and Anthropic's models return input-n-output tokens usage at the end of the streamed response.
Instead of implementing own low-level communication with LLM models, I'm aimed to integrate through Spring AI's abstraction.
Unfortunately, I've unsuccessfully tried high and low-level approaches that the framework provides to achieve the usage metadata. So, my question is how to get this metadata properly?
Here's some snippets if you wish:
` var messages = request.getMessages().stream()
.filter(m -> !m.role().equals("system"))
.map(m -> new AnthropicApi.RequestMessage(
List.of(new AnthropicApi.MediaContent((String) m.content())),
AnthropicApi.Role.valueOf(m.role().toUpperCase()))).toList();
// var chatCompletionMessage = new OpenAiApi.ChatCompletionMessage("Hello world", OpenAiApi.ChatCompletionMessage.Role.USER);
// Streaming request
Flux<AnthropicApi.StreamResponse> streamResponse = anthropicApi.chatCompletionStream(
new AnthropicApi.ChatCompletionRequest(
AnthropicApi.ChatModel.CLAUDE_3_OPUS.getValue(),
messages,
null,
request.getMaxTokens(),
0.8f,
true
)
);`
`var messages = request.getMessages().stream().map(m -> new OpenAiApi.ChatCompletionMessage(m.content(), OpenAiApi.ChatCompletionMessage.Role.valueOf(m.role().toUpperCase()))).toList();
// var chatCompletionMessage = new OpenAiApi.ChatCompletionMessage("Hello world", OpenAiApi.ChatCompletionMessage.Role.USER);
// Streaming request
Flux<ChatCompletionChunk> streamResponse = openAiApi.chatCompletionStream(
new OpenAiApi.ChatCompletionRequest(messages, request.getModel(), (float) request.getTemperature(), true));
//Prompt prompt = new Prompt(new UserMessage(message));
//return chatModel.stream(prompt);
return streamResponse;`
` var openAiChatOptions = OpenAiChatOptions.builder()
.withModel(request.getModel())
.withTemperature((float) request.getTemperature())
.withMaxTokens(request.getMaxTokens())
.build();
var chatModel = new OpenAiChatModel(openAiApi, openAiChatOptions);
var messages = request.getMessages().stream().map(m -> {
switch (MessageType.fromValue(m.role())) {
case USER -> {
return (Message) new UserMessage((String) m.content());
}
case ASSISTANT -> {
return (Message) new AssistantMessage((String) m.content());
}
case SYSTEM -> {
return (Message) new SystemMessage((String) m.content());
}
case FUNCTION -> {
return (Message) new FunctionMessage((String) m.content());
}
default -> throw new IllegalStateException("Unexpected value: " + MessageType.fromValue(m.role()));
}
//new Message(m.content(), OpenAiApi.ChatCompletionMessage.Role.valueOf(m.role().toUpperCase()))
}).toList();
Flux<ChatResponse> response = chatModel.stream(
new Prompt(messages));
return response; //.map(ChatResponse::getResult);`
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
First of all, my goal is to implement chatbot with billing based on the token usage as the original API does.
It's known that OpenAI's and Anthropic's models return input-n-output tokens usage at the end of the streamed response.
Instead of implementing own low-level communication with LLM models, I'm aimed to integrate through Spring AI's abstraction.
Unfortunately, I've unsuccessfully tried high and low-level approaches that the framework provides to achieve the usage metadata. So, my question is how to get this metadata properly?
Here's some snippets if you wish:
` var messages = request.getMessages().stream()
.filter(m -> !m.role().equals("system"))
.map(m -> new AnthropicApi.RequestMessage(
List.of(new AnthropicApi.MediaContent((String) m.content())),
AnthropicApi.Role.valueOf(m.role().toUpperCase()))).toList();
`var messages = request.getMessages().stream().map(m -> new OpenAiApi.ChatCompletionMessage(m.content(), OpenAiApi.ChatCompletionMessage.Role.valueOf(m.role().toUpperCase()))).toList();
` var openAiChatOptions = OpenAiChatOptions.builder()
.withModel(request.getModel())
.withTemperature((float) request.getTemperature())
.withMaxTokens(request.getMaxTokens())
.build();
var chatModel = new OpenAiChatModel(openAiApi, openAiChatOptions);
Beta Was this translation helpful? Give feedback.
All reactions