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

Fix Moonshot Chat model toolcalling token usage #1927

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ilayaperumalg
Copy link
Member

  • Accumulate the token usage when toolcalling is invoked
    • Fix both call() and stream() methods
      • Add usage field to the Chat completion choice as the usage is returned via Choice
  • Add Mootshot chatmodel ITs for functioncalling tests

 - Accumulate the token usage when toolcalling is invoked
   - Fix both call() and stream() methods
     - Add `usage` field to the Chat completion choice as the usage is returned via Choice
 - Add Mootshot chatmodel ITs for functioncalling tests
@ilayaperumalg
Copy link
Member Author

@mxsl-gr Could you check this PR?

@mxsl-gr
Copy link
Contributor

mxsl-gr commented Dec 17, 2024

@mxsl-gr Could you check this PR?

no problem, I'll check and test this PR later

@ilayaperumalg
Copy link
Member Author

no problem, I'll check and test this PR later

@mxsl-gr thank you

@mxsl-gr
Copy link
Contributor

mxsl-gr commented Dec 20, 2024

no problem, I'll check and test this PR later

@mxsl-gr thank you

hi, @ilayaperumalg
sorry for the delay due to some personal reasons. the unit tests work fine on my side, some suggestions for your reference.

@ilayaperumalg ilayaperumalg modified the milestones: 1.0.0-M5, 1.0.0-M6 Dec 21, 2024
@ilayaperumalg
Copy link
Member Author

@mxsl-gr I don't seem to find the suggestions on this PR. Could you check?


ChatResponse chatResponse = new ChatResponse(generations, from(completionEntity.getBody()));
MoonshotApi.Usage usage = completionEntity.getBody().usage();
Usage currentUsage = (usage != null) ? MoonshotUsage.from(usage) : new EmptyUsage();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this line of code seems unnecessary. MoonshotApi.Usage can directly implement the Usage interface, and UsageUtils.getCumulativeUsage already handles null checks for the input. This approach ensures that callers don’t need to repeatedly check for currentUsage in every call, avoiding redundant code and improving cohesion.
If an EmptyUsage is truly needed, it could simply be a static constant like Usage.empty() or EmptyUsage.instance().

@@ -286,8 +300,11 @@ public Flux<ChatResponse> stream(Prompt prompt) {
// @formatter:on
return buildGeneration(choice, metadata);
}).toList();
MoonshotApi.Usage usage = chatCompletion2.usage();
Usage currentUsage = (usage != null) ? MoonshotUsage.from(usage) : new EmptyUsage();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

*/
@SpringBootTest
@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".+")
public class MoonShotChatModelIT {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I recall there's a test class called MoonshotChatModelFunctionCallingIT that already contains unit tests for function calls. Instead of creating a new test class, maybe you could consider adding the new test methods to the existing one. And there is already a class org.springframework.ai.moonshot.chat.MoonshotChatModelIT

Also, the correct model name is Moonshot, not MoonShot.

@mxsl-gr
Copy link
Contributor

mxsl-gr commented Dec 23, 2024

@mxsl-gr I don't seem to find the suggestions on this PR. Could you check?

sorry, i forget finish the review...

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.

3 participants