|
| 1 | +/* |
| 2 | + * Copyright 2023-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.ai.moonshot; |
| 17 | + |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.List; |
| 20 | +import java.util.stream.Collectors; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; |
| 24 | +import reactor.core.publisher.Flux; |
| 25 | + |
| 26 | +import org.springframework.ai.chat.messages.AssistantMessage; |
| 27 | +import org.springframework.ai.chat.model.ChatResponse; |
| 28 | +import org.springframework.ai.chat.model.Generation; |
| 29 | +import org.springframework.ai.chat.prompt.Prompt; |
| 30 | +import org.springframework.ai.model.function.FunctionCallback; |
| 31 | +import org.springframework.ai.moonshot.api.MockWeatherService; |
| 32 | +import org.springframework.ai.moonshot.api.MoonshotApi; |
| 33 | +import org.springframework.beans.factory.annotation.Autowired; |
| 34 | +import org.springframework.boot.SpringBootConfiguration; |
| 35 | +import org.springframework.boot.test.context.SpringBootTest; |
| 36 | +import org.springframework.context.annotation.Bean; |
| 37 | +import org.springframework.util.StringUtils; |
| 38 | + |
| 39 | +import static org.assertj.core.api.Assertions.assertThat; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author Ilayaperumal Gopinathan |
| 43 | + */ |
| 44 | +@SpringBootTest |
| 45 | +@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".+") |
| 46 | +public class MoonShotChatModelIT { |
| 47 | + |
| 48 | + @Autowired |
| 49 | + private MoonshotChatModel chatModel; |
| 50 | + |
| 51 | + private static final MoonshotApi.FunctionTool FUNCTION_TOOL = new MoonshotApi.FunctionTool( |
| 52 | + MoonshotApi.FunctionTool.Type.FUNCTION, new MoonshotApi.FunctionTool.Function( |
| 53 | + "Get the weather in location. Return temperature in 30°F or 30°C format.", "getCurrentWeather", """ |
| 54 | + { |
| 55 | + "type": "object", |
| 56 | + "properties": { |
| 57 | + "location": { |
| 58 | + "type": "string", |
| 59 | + "description": "The city and state e.g. San Francisco, CA" |
| 60 | + }, |
| 61 | + "lat": { |
| 62 | + "type": "number", |
| 63 | + "description": "The city latitude" |
| 64 | + }, |
| 65 | + "lon": { |
| 66 | + "type": "number", |
| 67 | + "description": "The city longitude" |
| 68 | + }, |
| 69 | + "unit": { |
| 70 | + "type": "string", |
| 71 | + "enum": ["C", "F"] |
| 72 | + } |
| 73 | + }, |
| 74 | + "required": ["location", "lat", "lon", "unit"] |
| 75 | + } |
| 76 | + """)); |
| 77 | + |
| 78 | + @Test |
| 79 | + public void toolFunctionCall() { |
| 80 | + var promptOptions = MoonshotChatOptions.builder() |
| 81 | + .withModel(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue()) |
| 82 | + .withTools(Arrays.asList(FUNCTION_TOOL)) |
| 83 | + .withFunctionCallbacks(List.of(FunctionCallback.builder() |
| 84 | + .function("getCurrentWeather", new MockWeatherService()) |
| 85 | + .description("Get the weather in location. Return temperature in 36°F or 36°C format.") |
| 86 | + .inputType(MockWeatherService.Request.class) |
| 87 | + .build())) |
| 88 | + .build(); |
| 89 | + Prompt prompt = new Prompt("What's the weather like in San Francisco? Return the temperature in Celsius.", |
| 90 | + promptOptions); |
| 91 | + |
| 92 | + ChatResponse chatResponse = this.chatModel.call(prompt); |
| 93 | + assertThat(chatResponse).isNotNull(); |
| 94 | + assertThat(chatResponse.getResult().getOutput()); |
| 95 | + assertThat(chatResponse.getResult().getOutput().getText()).contains("San Francisco"); |
| 96 | + assertThat(chatResponse.getResult().getOutput().getText()).contains("30.0"); |
| 97 | + assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isLessThan(450).isGreaterThan(280); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testStreamFunctionCall() { |
| 102 | + var promptOptions = MoonshotChatOptions.builder() |
| 103 | + .withModel(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue()) |
| 104 | + .withTools(Arrays.asList(FUNCTION_TOOL)) |
| 105 | + .withFunctionCallbacks(List.of(FunctionCallback.builder() |
| 106 | + .function("getCurrentWeather", new MockWeatherService()) |
| 107 | + .description("Get the weather in location. Return temperature in 36°F or 36°C format.") |
| 108 | + .inputType(MockWeatherService.Request.class) |
| 109 | + .build())) |
| 110 | + .build(); |
| 111 | + Prompt prompt = new Prompt("What's the weather like in San Francisco? Return the temperature in Celsius.", |
| 112 | + promptOptions); |
| 113 | + |
| 114 | + Flux<ChatResponse> chatResponse = this.chatModel.stream(prompt); |
| 115 | + String content = chatResponse.collectList() |
| 116 | + .block() |
| 117 | + .stream() |
| 118 | + .map(ChatResponse::getResults) |
| 119 | + .flatMap(List::stream) |
| 120 | + .map(Generation::getOutput) |
| 121 | + .map(AssistantMessage::getText) |
| 122 | + .collect(Collectors.joining()); |
| 123 | + assertThat(content).contains("San Francisco"); |
| 124 | + assertThat(content).contains("30.0"); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testStreamFunctionCallUsage() { |
| 129 | + var promptOptions = MoonshotChatOptions.builder() |
| 130 | + .withModel(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue()) |
| 131 | + .withTools(Arrays.asList(FUNCTION_TOOL)) |
| 132 | + .withFunctionCallbacks(List.of(FunctionCallback.builder() |
| 133 | + .function("getCurrentWeather", new MockWeatherService()) |
| 134 | + .description("Get the weather in location. Return temperature in 36°F or 36°C format.") |
| 135 | + .inputType(MockWeatherService.Request.class) |
| 136 | + .build())) |
| 137 | + .build(); |
| 138 | + Prompt prompt = new Prompt("What's the weather like in San Francisco? Return the temperature in Celsius.", |
| 139 | + promptOptions); |
| 140 | + |
| 141 | + ChatResponse chatResponse = this.chatModel.stream(prompt).blockLast(); |
| 142 | + assertThat(chatResponse).isNotNull(); |
| 143 | + assertThat(chatResponse.getMetadata()).isNotNull(); |
| 144 | + assertThat(chatResponse.getMetadata().getUsage()).isNotNull(); |
| 145 | + assertThat(chatResponse.getMetadata().getUsage().getTotalTokens()).isLessThan(450).isGreaterThan(280); |
| 146 | + } |
| 147 | + |
| 148 | + @SpringBootConfiguration |
| 149 | + public static class Config { |
| 150 | + |
| 151 | + @Bean |
| 152 | + public MoonshotApi moonshotApi() { |
| 153 | + return new MoonshotApi(getApiKey()); |
| 154 | + } |
| 155 | + |
| 156 | + private String getApiKey() { |
| 157 | + String apiKey = System.getenv("MOONSHOT_API_KEY"); |
| 158 | + if (!StringUtils.hasText(apiKey)) { |
| 159 | + throw new IllegalArgumentException( |
| 160 | + "You must provide an API key. Put it in an environment variable under the name MOONSHOT_API_KEY"); |
| 161 | + } |
| 162 | + return apiKey; |
| 163 | + } |
| 164 | + |
| 165 | + @Bean |
| 166 | + public MoonshotChatModel moonshotChatModel(MoonshotApi api) { |
| 167 | + return new MoonshotChatModel(api); |
| 168 | + } |
| 169 | + |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments