stop) {
+ this.stop = stop;
+ }
+
+ @Override
+ public Float getTemperature() {
+ return this.temperature;
+ }
+
+ public void setTemperature(Float temperature) {
+ this.temperature = temperature;
+ }
+
+ @Override
+ public Float getTopP() {
+ return this.topP;
+ }
+
+ public void setTopP(Float topP) {
+ this.topP = topP;
+ }
+
+ public String getUser() {
+ return this.user;
+ }
+
+ public void setUser(String user) {
+ this.user = user;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result + ((model == null) ? 0 : model.hashCode());
+ result = prime * result + ((frequencyPenalty == null) ? 0 : frequencyPenalty.hashCode());
+ result = prime * result + ((maxTokens == null) ? 0 : maxTokens.hashCode());
+ result = prime * result + ((n == null) ? 0 : n.hashCode());
+ result = prime * result + ((presencePenalty == null) ? 0 : presencePenalty.hashCode());
+ result = prime * result + ((stop == null) ? 0 : stop.hashCode());
+ result = prime * result + ((temperature == null) ? 0 : temperature.hashCode());
+ result = prime * result + ((topP == null) ? 0 : topP.hashCode());
+ result = prime * result + ((user == null) ? 0 : user.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ MoonshotChatOptions other = (MoonshotChatOptions) obj;
+ if (this.model == null) {
+ if (other.model != null)
+ return false;
+ }
+ else if (!model.equals(other.model))
+ return false;
+ if (this.frequencyPenalty == null) {
+ if (other.frequencyPenalty != null)
+ return false;
+ }
+ else if (!this.frequencyPenalty.equals(other.frequencyPenalty))
+ return false;
+ if (this.maxTokens == null) {
+ if (other.maxTokens != null)
+ return false;
+ }
+ else if (!this.maxTokens.equals(other.maxTokens))
+ return false;
+ if (this.n == null) {
+ if (other.n != null)
+ return false;
+ }
+ else if (!this.n.equals(other.n))
+ return false;
+ if (this.presencePenalty == null) {
+ if (other.presencePenalty != null)
+ return false;
+ }
+ else if (!this.presencePenalty.equals(other.presencePenalty))
+ return false;
+ if (this.stop == null) {
+ if (other.stop != null)
+ return false;
+ }
+ else if (!stop.equals(other.stop))
+ return false;
+ if (this.temperature == null) {
+ if (other.temperature != null)
+ return false;
+ }
+ else if (!this.temperature.equals(other.temperature))
+ return false;
+ if (this.topP == null) {
+ if (other.topP != null)
+ return false;
+ }
+ else if (!topP.equals(other.topP))
+ return false;
+ if (this.user == null) {
+ return other.user == null;
+ }
+ else if (!this.user.equals(other.user))
+ return false;
+ return true;
+ }
+
+ @Override
+ @JsonIgnore
+ public Integer getTopK() {
+ throw new UnsupportedOperationException("Unimplemented method 'getTopK'");
+ }
+
+ @JsonIgnore
+ public void setTopK(Integer topK) {
+ throw new UnsupportedOperationException("Unimplemented method 'setTopK'");
+ }
+
+}
diff --git a/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/aot/MoonshotRuntimeHints.java b/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/aot/MoonshotRuntimeHints.java
new file mode 100644
index 00000000000..0ae4fccfe9a
--- /dev/null
+++ b/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/aot/MoonshotRuntimeHints.java
@@ -0,0 +1,42 @@
+/*
+ * 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.moonshot.aot;
+
+import org.springframework.ai.moonshot.api.MoonshotApi;
+import org.springframework.aot.hint.MemberCategory;
+import org.springframework.aot.hint.RuntimeHints;
+import org.springframework.aot.hint.RuntimeHintsRegistrar;
+import org.springframework.lang.NonNull;
+import org.springframework.lang.Nullable;
+
+import static org.springframework.ai.aot.AiRuntimeHints.findJsonAnnotatedClassesInPackage;
+
+/**
+ * The MoonshotRuntimeHints class is responsible for registering runtime hints for
+ * Moonshot API classes.
+ *
+ * @author Geng Rong
+ */
+public class MoonshotRuntimeHints implements RuntimeHintsRegistrar {
+
+ @Override
+ public void registerHints(@NonNull RuntimeHints hints, @Nullable ClassLoader classLoader) {
+ var mcs = MemberCategory.values();
+ for (var tr : findJsonAnnotatedClassesInPackage(MoonshotApi.class))
+ hints.reflection().registerType(tr, mcs);
+ }
+
+}
diff --git a/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/api/MoonshotApi.java b/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/api/MoonshotApi.java
new file mode 100644
index 00000000000..8c0a3ef4c5c
--- /dev/null
+++ b/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/api/MoonshotApi.java
@@ -0,0 +1,601 @@
+/*
+ * 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.moonshot.api;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import org.springframework.ai.model.ModelDescription;
+import org.springframework.ai.model.ModelOptionsUtils;
+import org.springframework.ai.retry.RetryUtils;
+import org.springframework.boot.context.properties.bind.ConstructorBinding;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.http.ResponseEntity;
+import org.springframework.util.Assert;
+import org.springframework.web.client.ResponseErrorHandler;
+import org.springframework.web.client.RestClient;
+import org.springframework.web.reactive.function.client.WebClient;
+import reactor.core.publisher.Flux;
+import reactor.core.publisher.Mono;
+
+import java.util.List;
+import java.util.Map;
+import java.util.function.Consumer;
+import java.util.function.Predicate;
+
+import static org.springframework.ai.moonshot.api.MoonshotConstants.DEFAULT_BASE_URL;
+
+/**
+ * Single-class, Java Client library for Moonshot platform. Provides implementation for
+ * the Chat Completion APIs.
+ *
+ * Implements Synchronous and Streaming chat completion.
+ *
+ *
+ * @author Geng Rong
+ */
+public class MoonshotApi {
+
+ public static final String DEFAULT_CHAT_MODEL = ChatModel.MOONSHOT_V1_32K.getValue();
+
+ private static final Predicate SSE_DONE_PREDICATE = "[DONE]"::equals;
+
+ private final RestClient restClient;
+
+ private final WebClient webClient;
+
+ /**
+ * Create a new client api with DEFAULT_BASE_URL
+ * @param moonshotApiKey Moonshot api Key.
+ */
+ public MoonshotApi(String moonshotApiKey) {
+ this(DEFAULT_BASE_URL, moonshotApiKey);
+ }
+
+ /**
+ * Create a new client api.
+ * @param baseUrl api base URL.
+ * @param moonshotApiKey Moonshot api Key.
+ */
+ public MoonshotApi(String baseUrl, String moonshotApiKey) {
+ this(baseUrl, moonshotApiKey, RestClient.builder(), RetryUtils.DEFAULT_RESPONSE_ERROR_HANDLER);
+ }
+
+ /**
+ * Create a new client api.
+ * @param baseUrl api base URL.
+ * @param moonshotApiKey Moonshot api Key.
+ * @param restClientBuilder RestClient builder.
+ * @param responseErrorHandler Response error handler.
+ */
+ public MoonshotApi(String baseUrl, String moonshotApiKey, RestClient.Builder restClientBuilder,
+ ResponseErrorHandler responseErrorHandler) {
+
+ Consumer jsonContentHeaders = headers -> {
+ headers.setBearerAuth(moonshotApiKey);
+ headers.setContentType(MediaType.APPLICATION_JSON);
+ };
+
+ this.restClient = restClientBuilder.baseUrl(baseUrl)
+ .defaultHeaders(jsonContentHeaders)
+ .defaultStatusHandler(responseErrorHandler)
+ .build();
+
+ this.webClient = WebClient.builder().baseUrl(baseUrl).defaultHeaders(jsonContentHeaders).build();
+ }
+
+ /**
+ * Usage statistics.
+ *
+ * @param promptTokens Number of tokens in the prompt.
+ * @param totalTokens Total number of tokens used in the request (prompt +
+ * completion).
+ * @param completionTokens Number of tokens in the generated completion. Only
+ * applicable for completion requests.
+ */
+ @JsonInclude(Include.NON_NULL)
+ public record Usage(
+ // @formatter:off
+ @JsonProperty("prompt_tokens") Integer promptTokens,
+ @JsonProperty("total_tokens") Integer totalTokens,
+ @JsonProperty("completion_tokens") Integer completionTokens) {
+ // @formatter:on
+ }
+
+ /**
+ * Creates a model response for the given chat conversation.
+ *
+ * @param model ID of the model to use.
+ * @param messages A list of messages comprising the conversation so far.
+ * @param maxTokens The maximum number of tokens to generate in the chat completion.
+ * The total length of input tokens and generated tokens is limited by the model's
+ * context length.
+ * @param temperature What sampling temperature to use, between 0 and 1. Higher values
+ * like 0.8 will make the output more random, while lower values like 0.2 will make it
+ * more focused and deterministic. We generally recommend altering this or top_p but
+ * not both.
+ * @param topP An alternative to sampling with temperature, called nucleus sampling,
+ * where the model considers the results of the tokens with top_p probability mass. So
+ * 0.1 means only the tokens comprising the top 10% probability mass are considered.
+ * We generally recommend altering this or temperature but not both.
+ * @param n How many chat completion choices to generate for each input message. Note
+ * that you will be charged based on the number of generated tokens across all the
+ * choices. Keep n as 1 to minimize costs.
+ * @param presencePenalty Number between -2.0 and 2.0. Positive values penalize new
+ * tokens based on whether they appear in the text so far, increasing the model's
+ * likelihood to talk about new topics.
+ * @param frequencyPenalty Number between -2.0 and 2.0. Positive values penalize new
+ * tokens based on their existing frequency in the text so far, decreasing the model's
+ * likelihood to repeat the same line verbatim.
+ * @param stop Up to 5 sequences where the API will stop generating further tokens.
+ * @param stream If set, partial message deltas will be sent.Tokens will be sent as
+ * data-only server-sent events as they become available, with the stream terminated
+ * by a data: [DONE] message.
+ */
+ @JsonInclude(Include.NON_NULL)
+ public record ChatCompletionRequest(
+ // @formatter:off
+ @JsonProperty("messages") List messages,
+ @JsonProperty("model") String model,
+ @JsonProperty("max_tokens") Integer maxTokens,
+ @JsonProperty("temperature") Float temperature,
+ @JsonProperty("top_p") Float topP,
+ @JsonProperty("n") Integer n,
+ @JsonProperty("frequency_penalty") Float frequencyPenalty,
+ @JsonProperty("presence_penalty") Float presencePenalty,
+ @JsonProperty("stop") List stop,
+ @JsonProperty("stream") Boolean stream,
+ @JsonProperty("tools") List tools,
+ @JsonProperty("tool_choice") Object toolChoice) {
+ // @formatter:on
+
+ /**
+ * Shortcut constructor for a chat completion request with the given messages and
+ * model.
+ * @param messages The prompt(s) to generate completions for, encoded as a list of
+ * dict with role and content. The first prompt role should be user or system.
+ * @param model ID of the model to use.
+ */
+ public ChatCompletionRequest(List messages, String model) {
+ this(messages, model, null, 0.3f, 1f, null, null, null, null, false, null, null);
+ }
+
+ /**
+ * Shortcut constructor for a chat completion request with the given messages,
+ * model and temperature.
+ * @param messages The prompt(s) to generate completions for, encoded as a list of
+ * dict with role and content. The first prompt role should be user or system.
+ * @param model ID of the model to use.
+ * @param temperature What sampling temperature to use, between 0.0 and 1.0.
+ * @param stream Whether to stream back partial progress. If set, tokens will be
+ * sent
+ */
+ public ChatCompletionRequest(List messages, String model, Float temperature,
+ boolean stream) {
+ this(messages, model, null, temperature, 1f, null, null, null, null, stream, null, null);
+ }
+
+ /**
+ * Shortcut constructor for a chat completion request with the given messages,
+ * model and temperature.
+ * @param messages The prompt(s) to generate completions for, encoded as a list of
+ * dict with role and content. The first prompt role should be user or system.
+ * @param model ID of the model to use.
+ * @param temperature What sampling temperature to use, between 0.0 and 1.0.
+ */
+ public ChatCompletionRequest(List messages, String model, Float temperature) {
+ this(messages, model, null, temperature, 1f, null, null, null, null, false, null, null);
+ }
+
+ /**
+ * Shortcut constructor for a chat completion request with the given messages,
+ * model, tools and tool choice. Streaming is set to false, temperature to 0.8 and
+ * all other parameters are null.
+ * @param messages A list of messages comprising the conversation so far.
+ * @param model ID of the model to use.
+ * @param tools A list of tools the model may call. Currently, only functions are
+ * supported as a tool.
+ * @param toolChoice Controls which (if any) function is called by the model.
+ */
+ public ChatCompletionRequest(List messages, String model, List tools,
+ Object toolChoice) {
+ this(messages, model, null, null, 1f, null, null, null, null, false, tools, toolChoice);
+ }
+
+ /**
+ * Shortcut constructor for a chat completion request with the given messages and
+ * stream.
+ */
+ public ChatCompletionRequest(List messages, Boolean stream) {
+ this(messages, DEFAULT_CHAT_MODEL, null, 0.7f, 1F, null, null, null, null, stream, null, null);
+ }
+
+ /**
+ * Helper factory that creates a tool_choice of type 'none', 'auto' or selected
+ * function by name.
+ */
+ public static class ToolChoiceBuilder {
+
+ /**
+ * Model can pick between generating a message or calling a function.
+ */
+ public static final String AUTO = "auto";
+
+ /**
+ * Model will not call a function and instead generates a message
+ */
+ public static final String NONE = "none";
+
+ /**
+ * Specifying a particular function forces the model to call that function.
+ */
+ public static Object function(String functionName) {
+ return Map.of("type", "function", "function", Map.of("name", functionName));
+ }
+
+ }
+ }
+
+ /**
+ * Message comprising the conversation.
+ *
+ * @param content The contents of the message.
+ * @param role The role of the messages author. Could be one of the {@link Role}
+ * types.
+ */
+ @JsonInclude(Include.NON_NULL)
+ public record ChatCompletionMessage(
+ // @formatter:off
+ @JsonProperty("content") Object rawContent,
+ @JsonProperty("role") Role role,
+ @JsonProperty("name") String name,
+ @JsonProperty("tool_call_id") String toolCallId,
+ @JsonProperty("tool_calls") List toolCalls
+ // @formatter:on
+ ) {
+
+ /**
+ * Get message content as String.
+ */
+ public String content() {
+ if (this.rawContent == null) {
+ return null;
+ }
+ if (this.rawContent instanceof String text) {
+ return text;
+ }
+ throw new IllegalStateException("The content is not a string!");
+ }
+
+ /**
+ * Create a chat completion message with the given content and role. All other
+ * fields are null.
+ * @param content The contents of the message.
+ * @param role The role of the author of this message.
+ */
+ public ChatCompletionMessage(Object content, Role role) {
+ this(content, role, null, null, null);
+ }
+
+ /**
+ * The role of the author of this message. NOTE: Moonshot expects the system
+ * message to be before the user message or will fail with 400 error.
+ */
+ public enum Role {
+
+ /**
+ * System message.
+ */
+ @JsonProperty("system")
+ SYSTEM,
+ /**
+ * User message.
+ */
+ @JsonProperty("user")
+ USER,
+ /**
+ * Assistant message.
+ */
+ @JsonProperty("assistant")
+ ASSISTANT,
+ /**
+ * Tool message.
+ */
+ @JsonProperty("tool")
+ TOOL
+ // @formatter:on
+
+ }
+
+ /**
+ * The relevant tool call.
+ *
+ * @param id The ID of the tool call. This ID must be referenced when you submit
+ * the tool outputs in using the Submit tool outputs to run endpoint.
+ * @param type The type of tool call the output is required for. For now, this is
+ * always function.
+ * @param function The function definition.
+ */
+ @JsonInclude(Include.NON_NULL)
+ public record ToolCall(@JsonProperty("id") String id, @JsonProperty("type") String type,
+ @JsonProperty("function") ChatCompletionFunction function) {
+ }
+
+ /**
+ * The function definition.
+ *
+ * @param name The name of the function.
+ * @param arguments The arguments that the model expects you to pass to the
+ * function.
+ */
+ @JsonInclude(Include.NON_NULL)
+ public record ChatCompletionFunction(@JsonProperty("name") String name,
+ @JsonProperty("arguments") String arguments) {
+ }
+ }
+
+ /**
+ * The reason the model stopped generating tokens.
+ */
+ public enum ChatCompletionFinishReason {
+
+ /**
+ * The model hit a natural stop point or a provided stop sequence.
+ */
+ @JsonProperty("stop")
+ STOP,
+ /**
+ * The maximum number of tokens specified in the request was reached.
+ */
+ @JsonProperty("length")
+ LENGTH,
+ /**
+ * The content was omitted due to a flag from our content filters.
+ */
+ @JsonProperty("content_filter")
+ CONTENT_FILTER,
+ /**
+ * The model called a tool.
+ */
+ @JsonProperty("tool_calls")
+ TOOL_CALLS,
+ /**
+ * (deprecated) The model called a function.
+ */
+ @JsonProperty("function_call")
+ FUNCTION_CALL,
+ /**
+ * Only for compatibility with Mistral AI API.
+ */
+ @JsonProperty("tool_call")
+ TOOL_CALL
+
+ }
+
+ /**
+ * Represents a chat completion response returned by model, based on the provided
+ * input.
+ *
+ * @param id A unique identifier for the chat completion.
+ * @param object The object type, which is always chat.completion.
+ * @param created The Unix timestamp (in seconds) of when the chat completion was
+ * created.
+ * @param model The model used for the chat completion.
+ * @param choices A list of chat completion choices.
+ * @param usage Usage statistics for the completion request.
+ */
+ @JsonInclude(Include.NON_NULL)
+ public record ChatCompletion(
+ // @formatter:off
+ @JsonProperty("id") String id,
+ @JsonProperty("object") String object,
+ @JsonProperty("created") Long created,
+ @JsonProperty("model") String model,
+ @JsonProperty("choices") List choices,
+ @JsonProperty("usage") Usage usage) {
+ // @formatter:on
+
+ /**
+ * Chat completion choice.
+ *
+ * @param index The index of the choice in the list of choices.
+ * @param message A chat completion message generated by the model.
+ * @param finishReason The reason the model stopped generating tokens.
+ */
+ @JsonInclude(Include.NON_NULL)
+ public record Choice(
+ // @formatter:off
+ @JsonProperty("index") Integer index,
+ @JsonProperty("message") ChatCompletionMessage message,
+ @JsonProperty("finish_reason") ChatCompletionFinishReason finishReason) {
+ // @formatter:on
+ }
+ }
+
+ /**
+ * Represents a streamed chunk of a chat completion response returned by model, based
+ * on the provided input.
+ *
+ * @param id A unique identifier for the chat completion. Each chunk has the same ID.
+ * @param object The object type, which is always 'chat.completion.chunk'.
+ * @param created The Unix timestamp (in seconds) of when the chat completion was
+ * created. Each chunk has the same timestamp.
+ * @param model The model used for the chat completion.
+ * @param choices A list of chat completion choices. Can be more than one if n is
+ * greater than 1.
+ */
+ @JsonInclude(Include.NON_NULL)
+ public record ChatCompletionChunk(
+ // @formatter:off
+ @JsonProperty("id") String id,
+ @JsonProperty("object") String object,
+ @JsonProperty("created") Long created,
+ @JsonProperty("model") String model,
+ @JsonProperty("choices") List choices) {
+ // @formatter:on
+
+ /**
+ * Chat completion choice.
+ *
+ * @param index The index of the choice in the list of choices.
+ * @param delta A chat completion delta generated by streamed model responses.
+ * @param finishReason The reason the model stopped generating tokens.
+ */
+ @JsonInclude(Include.NON_NULL)
+ public record ChunkChoice(
+ // @formatter:off
+ @JsonProperty("index") Integer index,
+ @JsonProperty("delta") ChatCompletionMessage delta,
+ @JsonProperty("finish_reason") ChatCompletionFinishReason finishReason,
+ @JsonProperty("usage") Usage usage
+ // @formatter:on
+ ) {
+ }
+ }
+
+ /**
+ * Moonshot Chat Completion Models:
+ *
+ *
+ * - MOONSHOT_V1_8K - moonshot-v1-8k
+ * - MOONSHOT_V1_32K - moonshot-v1-32k
+ * - MOONSHOT_V1_128K - moonshot-v1-128k
+ *
+ */
+ public enum ChatModel implements ModelDescription {
+
+ // @formatter:off
+ MOONSHOT_V1_8K("moonshot-v1-8k"),
+ MOONSHOT_V1_32K("moonshot-v1-32k"),
+ MOONSHOT_V1_128K("moonshot-v1-128k");
+ // @formatter:on
+
+ private final String value;
+
+ ChatModel(String value) {
+ this.value = value;
+ }
+
+ public String getValue() {
+ return this.value;
+ }
+
+ @Override
+ public String getModelName() {
+ return this.value;
+ }
+
+ }
+
+ /**
+ * Represents a tool the model may call. Currently, only functions are supported as a
+ * tool.
+ *
+ * @param type The type of the tool. Currently, only 'function' is supported.
+ * @param function The function definition.
+ */
+ @JsonInclude(Include.NON_NULL)
+ public record FunctionTool(@JsonProperty("type") Type type, @JsonProperty("function") Function function) {
+
+ /**
+ * Create a tool of type 'function' and the given function definition.
+ * @param function function definition.
+ */
+ @ConstructorBinding
+ public FunctionTool(Function function) {
+ this(Type.FUNCTION, function);
+ }
+
+ /**
+ * Create a tool of type 'function' and the given function definition.
+ */
+ public enum Type {
+
+ /**
+ * Function tool type.
+ */
+ @JsonProperty("function")
+ FUNCTION
+
+ }
+
+ /**
+ * Function definition.
+ *
+ * @param description A description of what the function does, used by the model
+ * to choose when and how to call the function.
+ * @param name The name of the function to be called. Must be a-z, A-Z, 0-9, or
+ * contain underscores and dashes, with a maximum length of 64.
+ * @param parameters The parameters the functions accepts, described as a JSON
+ * Schema object. To describe a function that accepts no parameters, provide the
+ * value {"type": "object", "properties": {}}.
+ */
+ public record Function(@JsonProperty("description") String description, @JsonProperty("name") String name,
+ @JsonProperty("parameters") Map parameters) {
+
+ /**
+ * Create tool function definition.
+ * @param description tool function description.
+ * @param name tool function name.
+ * @param jsonSchema tool function schema as json.
+ */
+ @ConstructorBinding
+ public Function(String description, String name, String jsonSchema) {
+ this(description, name, ModelOptionsUtils.jsonToMap(jsonSchema));
+ }
+ }
+ }
+
+ /**
+ * Creates a model response for the given chat conversation.
+ * @param chatRequest The chat completion request.
+ * @return Entity response with {@link ChatCompletion} as a body and HTTP status code
+ * and headers.
+ */
+ public ResponseEntity chatCompletionEntity(ChatCompletionRequest chatRequest) {
+
+ Assert.notNull(chatRequest, "The request body can not be null.");
+ Assert.isTrue(!chatRequest.stream(), "Request must set the steam property to false.");
+
+ return this.restClient.post()
+ .uri("/v1/chat/completions")
+ .body(chatRequest)
+ .retrieve()
+ .toEntity(ChatCompletion.class);
+ }
+
+ /**
+ * Creates a streaming chat response for the given chat conversation.
+ * @param chatRequest The chat completion request. Must have the stream property set
+ * to true.
+ * @return Returns a {@link Flux} stream from chat completion chunks.
+ */
+ public Flux chatCompletionStream(ChatCompletionRequest chatRequest) {
+ Assert.notNull(chatRequest, "The request body can not be null.");
+ Assert.isTrue(chatRequest.stream(), "Request must set the steam property to true.");
+
+ return this.webClient.post()
+ .uri("/v1/chat/completions")
+ .body(Mono.just(chatRequest), ChatCompletionRequest.class)
+ .retrieve()
+ .bodyToFlux(String.class)
+ .takeUntil(SSE_DONE_PREDICATE)
+ .filter(SSE_DONE_PREDICATE.negate())
+ .map(content -> ModelOptionsUtils.jsonToObject(content, ChatCompletionChunk.class));
+ }
+
+}
diff --git a/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/api/MoonshotConstants.java b/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/api/MoonshotConstants.java
new file mode 100644
index 00000000000..9e28af3fa51
--- /dev/null
+++ b/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/api/MoonshotConstants.java
@@ -0,0 +1,25 @@
+/*
+ * 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.moonshot.api;
+
+/**
+ * @author Geng Rong
+ */
+public final class MoonshotConstants {
+
+ public static final String DEFAULT_BASE_URL = "https://api.moonshot.cn";
+
+}
diff --git a/models/spring-ai-moonshot/src/main/resources/META-INF/spring/aot.factories b/models/spring-ai-moonshot/src/main/resources/META-INF/spring/aot.factories
new file mode 100644
index 00000000000..1f4c13ec6ae
--- /dev/null
+++ b/models/spring-ai-moonshot/src/main/resources/META-INF/spring/aot.factories
@@ -0,0 +1,2 @@
+org.springframework.aot.hint.RuntimeHintsRegistrar=\
+ org.springframework.ai.moonshot.aot.MoonshotRuntimeHints
\ No newline at end of file
diff --git a/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotChatCompletionRequestTest.java b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotChatCompletionRequestTest.java
new file mode 100644
index 00000000000..568c4142e1e
--- /dev/null
+++ b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotChatCompletionRequestTest.java
@@ -0,0 +1,57 @@
+/*
+ * 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.moonshot;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.moonshot.api.MoonshotApi;
+import org.springframework.boot.test.context.SpringBootTest;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@SpringBootTest
+@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".+")
+public class MoonshotChatCompletionRequestTest {
+
+ MoonshotChatModel chatModel = new MoonshotChatModel(new MoonshotApi("test"));
+
+ @Test
+ void chatCompletionDefaultRequestTest() {
+ var request = chatModel.createRequest(new Prompt("test content"), false);
+
+ assertThat(request.messages()).hasSize(1);
+ assertThat(request.topP()).isEqualTo(1);
+ assertThat(request.temperature()).isEqualTo(0.7f);
+ assertThat(request.maxTokens()).isNull();
+ assertThat(request.stream()).isFalse();
+ }
+
+ @Test
+ void chatCompletionRequestWithOptionsTest() {
+ var options = MoonshotChatOptions.builder().withTemperature(0.5f).withTopP(0.8f).build();
+ var request = chatModel.createRequest(new Prompt("test content", options), true);
+
+ assertThat(request.messages().size()).isEqualTo(1);
+ assertThat(request.topP()).isEqualTo(0.8f);
+ assertThat(request.temperature()).isEqualTo(0.5f);
+ assertThat(request.stream()).isTrue();
+ }
+
+}
diff --git a/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotRetryTests.java b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotRetryTests.java
new file mode 100644
index 00000000000..48bd899ffe8
--- /dev/null
+++ b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotRetryTests.java
@@ -0,0 +1,151 @@
+/*
+ * 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.moonshot;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.moonshot.api.MoonshotApi;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletion;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionChunk;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionFinishReason;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionMessage;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionMessage.Role;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionRequest;
+import org.springframework.ai.retry.RetryUtils;
+import org.springframework.ai.retry.TransientAiException;
+import org.springframework.http.ResponseEntity;
+import org.springframework.retry.RetryCallback;
+import org.springframework.retry.RetryContext;
+import org.springframework.retry.RetryListener;
+import org.springframework.retry.support.RetryTemplate;
+import reactor.core.publisher.Flux;
+
+import java.util.List;
+import java.util.Optional;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.ArgumentMatchers.isA;
+import static org.mockito.Mockito.when;
+
+/**
+ * @author Geng Rong
+ */
+@SuppressWarnings("unchecked")
+@ExtendWith(MockitoExtension.class)
+public class MoonshotRetryTests {
+
+ private static class TestRetryListener implements RetryListener {
+
+ int onErrorRetryCount = 0;
+
+ int onSuccessRetryCount = 0;
+
+ @Override
+ public void onSuccess(RetryContext context, RetryCallback callback, T result) {
+ onSuccessRetryCount = context.getRetryCount();
+ }
+
+ @Override
+ public void onError(RetryContext context, RetryCallback callback,
+ Throwable throwable) {
+ onErrorRetryCount = context.getRetryCount();
+ }
+
+ }
+
+ private TestRetryListener retryListener;
+
+ private @Mock MoonshotApi moonshotApi;
+
+ private MoonshotChatModel chatModel;
+
+ @BeforeEach
+ public void beforeEach() {
+ RetryTemplate retryTemplate = RetryUtils.DEFAULT_RETRY_TEMPLATE;
+ retryListener = new TestRetryListener();
+ retryTemplate.registerListener(retryListener);
+
+ chatModel = new MoonshotChatModel(moonshotApi,
+ MoonshotChatOptions.builder()
+ .withTemperature(0.7f)
+ .withTopP(1f)
+ .withModel(MoonshotApi.ChatModel.MOONSHOT_V1_32K.getValue())
+ .build(),
+ retryTemplate);
+ }
+
+ @Test
+ public void moonshotChatTransientError() {
+
+ var choice = new ChatCompletion.Choice(0, new ChatCompletionMessage("Response", Role.ASSISTANT),
+ ChatCompletionFinishReason.STOP);
+ ChatCompletion expectedChatCompletion = new ChatCompletion("id", "chat.completion", 789l, "model",
+ List.of(choice), new MoonshotApi.Usage(10, 10, 10));
+
+ when(moonshotApi.chatCompletionEntity(isA(ChatCompletionRequest.class)))
+ .thenThrow(new TransientAiException("Transient Error 1"))
+ .thenThrow(new TransientAiException("Transient Error 2"))
+ .thenReturn(ResponseEntity.of(Optional.of(expectedChatCompletion)));
+
+ var result = chatModel.call(new Prompt("text"));
+
+ assertThat(result).isNotNull();
+ assertThat(result.getResult().getOutput().getContent()).isSameAs("Response");
+ assertThat(retryListener.onSuccessRetryCount).isEqualTo(2);
+ assertThat(retryListener.onErrorRetryCount).isEqualTo(2);
+ }
+
+ @Test
+ public void moonshotChatNonTransientError() {
+ when(moonshotApi.chatCompletionEntity(isA(ChatCompletionRequest.class)))
+ .thenThrow(new RuntimeException("Non Transient Error"));
+ assertThrows(RuntimeException.class, () -> chatModel.call(new Prompt("text")));
+ }
+
+ @Test
+ public void moonshotChatStreamTransientError() {
+
+ var choice = new ChatCompletionChunk.ChunkChoice(0, new ChatCompletionMessage("Response", Role.ASSISTANT),
+ ChatCompletionFinishReason.STOP, null);
+ ChatCompletionChunk expectedChatCompletion = new ChatCompletionChunk("id", "chat.completion.chunk", 789l,
+ "model", List.of(choice));
+
+ when(moonshotApi.chatCompletionStream(isA(ChatCompletionRequest.class)))
+ .thenThrow(new TransientAiException("Transient Error 1"))
+ .thenThrow(new TransientAiException("Transient Error 2"))
+ .thenReturn(Flux.just(expectedChatCompletion));
+
+ var result = chatModel.stream(new Prompt("text"));
+
+ assertThat(result).isNotNull();
+ assertThat(result.collectList().block().get(0).getResult().getOutput().getContent()).isSameAs("Response");
+ assertThat(retryListener.onSuccessRetryCount).isEqualTo(2);
+ assertThat(retryListener.onErrorRetryCount).isEqualTo(2);
+ }
+
+ @Test
+ public void moonshotChatStreamNonTransientError() {
+ when(moonshotApi.chatCompletionStream(isA(ChatCompletionRequest.class)))
+ .thenThrow(new RuntimeException("Non Transient Error"));
+ assertThrows(RuntimeException.class, () -> chatModel.stream(new Prompt("text")));
+ }
+
+}
diff --git a/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotTestConfiguration.java b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotTestConfiguration.java
new file mode 100644
index 00000000000..11db99be985
--- /dev/null
+++ b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/MoonshotTestConfiguration.java
@@ -0,0 +1,47 @@
+/*
+ * 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.moonshot;
+
+import org.springframework.ai.moonshot.api.MoonshotApi;
+import org.springframework.boot.SpringBootConfiguration;
+import org.springframework.context.annotation.Bean;
+import org.springframework.util.StringUtils;
+
+/**
+ * @author Geng Rong
+ */
+@SpringBootConfiguration
+public class MoonshotTestConfiguration {
+
+ @Bean
+ public MoonshotApi moonshotApi() {
+ var apiKey = System.getenv("MOONSHOT_API_KEY");
+ if (!StringUtils.hasText(apiKey)) {
+ throw new IllegalArgumentException(
+ "Missing MOONSHOT_API_KEY environment variable. Please set it to your Moonshot API key.");
+ }
+ return new MoonshotApi(apiKey);
+ }
+
+ @Bean
+ public MoonshotChatModel moonshotChatModel(MoonshotApi moonshotApi) {
+ return new MoonshotChatModel(moonshotApi);
+ }
+
+ public void tst() {
+ }
+
+}
diff --git a/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/aot/MoonshotRuntimeHintsTests.java b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/aot/MoonshotRuntimeHintsTests.java
new file mode 100644
index 00000000000..e6015951db6
--- /dev/null
+++ b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/aot/MoonshotRuntimeHintsTests.java
@@ -0,0 +1,46 @@
+/*
+ * 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.moonshot.aot;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.ai.moonshot.api.MoonshotApi;
+import org.springframework.aot.hint.RuntimeHints;
+import org.springframework.aot.hint.TypeReference;
+
+import java.util.Set;
+
+import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
+import static org.springframework.ai.aot.AiRuntimeHints.findJsonAnnotatedClassesInPackage;
+import static org.springframework.aot.hint.predicate.RuntimeHintsPredicates.reflection;
+
+/**
+ * @author Geng Rong
+ */
+class MoonshotRuntimeHintsTests {
+
+ @Test
+ void registerHints() {
+ RuntimeHints runtimeHints = new RuntimeHints();
+ MoonshotRuntimeHints moonshotRuntimeHints = new MoonshotRuntimeHints();
+ moonshotRuntimeHints.registerHints(runtimeHints, null);
+
+ Set jsonAnnotatedClasses = findJsonAnnotatedClassesInPackage(MoonshotApi.class);
+ for (TypeReference jsonAnnotatedClass : jsonAnnotatedClasses) {
+ assertThat(runtimeHints).matches(reflection().onType(jsonAnnotatedClass));
+ }
+ }
+
+}
diff --git a/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MockWeatherService.java b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MockWeatherService.java
new file mode 100644
index 00000000000..145fe24ea7a
--- /dev/null
+++ b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MockWeatherService.java
@@ -0,0 +1,92 @@
+/*
+ * 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.moonshot.api;
+
+import com.fasterxml.jackson.annotation.JsonClassDescription;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyDescription;
+
+import java.util.function.Function;
+
+/**
+ * @author Geng Rong
+ */
+public class MockWeatherService implements Function {
+
+ /**
+ * Weather Function request.
+ */
+ @JsonInclude(Include.NON_NULL)
+ @JsonClassDescription("Weather API request")
+ public record Request(@JsonProperty(required = true,
+ value = "location") @JsonPropertyDescription("The city and state e.g. San Francisco, CA") String location,
+ @JsonProperty(required = true, value = "lat") @JsonPropertyDescription("The city latitude") double lat,
+ @JsonProperty(required = true, value = "lon") @JsonPropertyDescription("The city longitude") double lon,
+ @JsonProperty(required = true, value = "unit") @JsonPropertyDescription("Temperature unit") Unit unit) {
+ }
+
+ /**
+ * Temperature units.
+ */
+ public enum Unit {
+
+ /**
+ * Celsius.
+ */
+ C("metric"),
+ /**
+ * Fahrenheit.
+ */
+ F("imperial");
+
+ /**
+ * Human readable unit name.
+ */
+ public final String unitName;
+
+ private Unit(String text) {
+ this.unitName = text;
+ }
+
+ }
+
+ /**
+ * Weather Function response.
+ */
+ public record Response(double temp, double feels_like, double temp_min, double temp_max, int pressure, int humidity,
+ Unit unit) {
+ }
+
+ @Override
+ public Response apply(Request request) {
+
+ double temperature = 0;
+ if (request.location().contains("Paris")) {
+ temperature = 15;
+ }
+ else if (request.location().contains("Tokyo")) {
+ temperature = 10;
+ }
+ else if (request.location().contains("San Francisco")) {
+ temperature = 30;
+ }
+
+ return new Response(temperature, 15, 20, 2, 53, 45, request.unit);
+ }
+
+}
\ No newline at end of file
diff --git a/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MoonshotApiIT.java b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MoonshotApiIT.java
new file mode 100644
index 00000000000..9498b38d997
--- /dev/null
+++ b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MoonshotApiIT.java
@@ -0,0 +1,77 @@
+/*
+ * 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.moonshot.api;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletion;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionChunk;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionMessage;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionMessage.Role;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionRequest;
+import org.springframework.http.ResponseEntity;
+import reactor.core.publisher.Flux;
+
+import java.util.List;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".+")
+public class MoonshotApiIT {
+
+ MoonshotApi moonshotApi = new MoonshotApi(System.getenv("MOONSHOT_API_KEY"));
+
+ @Test
+ void chatCompletionEntity() {
+ ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER);
+ ResponseEntity response = moonshotApi.chatCompletionEntity(new ChatCompletionRequest(
+ List.of(chatCompletionMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.8f, false));
+
+ assertThat(response).isNotNull();
+ assertThat(response.getBody()).isNotNull();
+ }
+
+ @Test
+ void chatCompletionEntityWithSystemMessage() {
+ ChatCompletionMessage userMessage = new ChatCompletionMessage(
+ "Tell me about 3 famous pirates from the Golden Age of Piracy and why they did?", Role.USER);
+ ChatCompletionMessage systemMessage = new ChatCompletionMessage("""
+ You are an AI assistant that helps people find information.
+ Your name is Bob.
+ You should reply to the user's request with your name and also in the style of a pirate.
+ """, Role.SYSTEM);
+
+ ResponseEntity response = moonshotApi.chatCompletionEntity(new ChatCompletionRequest(
+ List.of(systemMessage, userMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.8f, false));
+
+ assertThat(response).isNotNull();
+ assertThat(response.getBody()).isNotNull();
+ }
+
+ @Test
+ void chatCompletionStream() {
+ ChatCompletionMessage chatCompletionMessage = new ChatCompletionMessage("Hello world", Role.USER);
+ Flux response = moonshotApi.chatCompletionStream(new ChatCompletionRequest(
+ List.of(chatCompletionMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.8f, true));
+
+ assertThat(response).isNotNull();
+ assertThat(response.collectList().block()).isNotNull();
+ }
+
+}
diff --git a/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MoonshotApiToolFunctionCallIT.java b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MoonshotApiToolFunctionCallIT.java
new file mode 100644
index 00000000000..b99f6e7af5d
--- /dev/null
+++ b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MoonshotApiToolFunctionCallIT.java
@@ -0,0 +1,140 @@
+/*
+ * 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.moonshot.api;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletion;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionMessage;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionMessage.Role;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionMessage.ToolCall;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionRequest;
+import org.springframework.ai.moonshot.api.MoonshotApi.ChatCompletionRequest.ToolChoiceBuilder;
+import org.springframework.ai.moonshot.api.MoonshotApi.FunctionTool.Type;
+import org.springframework.http.ResponseEntity;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Objects;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".+")
+public class MoonshotApiToolFunctionCallIT {
+
+ private final Logger logger = LoggerFactory.getLogger(MoonshotApiToolFunctionCallIT.class);
+
+ MockWeatherService weatherService = new MockWeatherService();
+
+ MoonshotApi moonshotApi = new MoonshotApi(System.getenv("MOONSHOT_API_KEY"));
+
+ @SuppressWarnings("null")
+ @Test
+ public void toolFunctionCall() {
+
+ // Step 1: send the conversation and available functions to the model
+ var message = new ChatCompletionMessage("What's the weather like in San Francisco?", Role.USER);
+
+ var functionTool = new MoonshotApi.FunctionTool(Type.FUNCTION, new MoonshotApi.FunctionTool.Function(
+ "Get the weather in location. Return temperature in 30°F or 30°C format.", "getCurrentWeather", """
+ {
+ "type": "object",
+ "properties": {
+ "location": {
+ "type": "string",
+ "description": "The city and state e.g. San Francisco, CA"
+ },
+ "lat": {
+ "type": "number",
+ "description": "The city latitude"
+ },
+ "lon": {
+ "type": "number",
+ "description": "The city longitude"
+ },
+ "unit": {
+ "type": "string",
+ "enum": ["C", "F"]
+ }
+ },
+ "required": ["location", "lat", "lon", "unit"]
+ }
+ """));
+
+ List messages = new ArrayList<>(List.of(message));
+
+ ChatCompletionRequest chatCompletionRequest = new ChatCompletionRequest(messages,
+ MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), List.of(functionTool), ToolChoiceBuilder.AUTO);
+
+ ResponseEntity chatCompletion = moonshotApi.chatCompletionEntity(chatCompletionRequest);
+
+ assertThat(chatCompletion.getBody()).isNotNull();
+ assertThat(chatCompletion.getBody().choices()).isNotEmpty();
+
+ ChatCompletionMessage responseMessage = chatCompletion.getBody().choices().get(0).message();
+
+ assertThat(responseMessage.role()).isEqualTo(Role.ASSISTANT);
+ assertThat(responseMessage.toolCalls()).isNotNull();
+
+ messages.add(responseMessage);
+
+ // Send the info for each function call and function response to the model.
+ for (ToolCall toolCall : responseMessage.toolCalls()) {
+ var functionName = toolCall.function().name();
+ if ("getCurrentWeather".equals(functionName)) {
+ MockWeatherService.Request weatherRequest = fromJson(toolCall.function().arguments(),
+ MockWeatherService.Request.class);
+
+ MockWeatherService.Response weatherResponse = weatherService.apply(weatherRequest);
+
+ // extend conversation with function response.
+ messages.add(new ChatCompletionMessage("" + weatherResponse.temp() + weatherRequest.unit(), Role.TOOL,
+ functionName, toolCall.id(), null));
+ }
+ }
+
+ var functionResponseRequest = new ChatCompletionRequest(messages,
+ MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.5F);
+
+ ResponseEntity chatCompletion2 = moonshotApi.chatCompletionEntity(functionResponseRequest);
+
+ logger.info("Final response: " + chatCompletion2.getBody());
+
+ assertThat(Objects.requireNonNull(chatCompletion2.getBody()).choices()).isNotEmpty();
+
+ assertThat(chatCompletion2.getBody().choices().get(0).message().role()).isEqualTo(Role.ASSISTANT);
+ assertThat(chatCompletion2.getBody().choices().get(0).message().content()).contains("San Francisco")
+ .containsAnyOf("30.0°C", "30°C", "30.0°F", "30°F", "30.0", "30");
+ }
+
+ private static T fromJson(String json, Class targetClass) {
+ try {
+ return new ObjectMapper().readValue(json, targetClass);
+ }
+ catch (JsonProcessingException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/chat/ActorsFilms.java b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/chat/ActorsFilms.java
new file mode 100644
index 00000000000..d4436cbb7d7
--- /dev/null
+++ b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/chat/ActorsFilms.java
@@ -0,0 +1,53 @@
+/*
+ * 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.moonshot.chat;
+
+import java.util.List;
+
+/**
+ * @author Geng Rong
+ */
+public class ActorsFilms {
+
+ private String actor;
+
+ private List movies;
+
+ public ActorsFilms() {
+ }
+
+ public String getActor() {
+ return actor;
+ }
+
+ public void setActor(String actor) {
+ this.actor = actor;
+ }
+
+ public List getMovies() {
+ return movies;
+ }
+
+ public void setMovies(List movies) {
+ this.movies = movies;
+ }
+
+ @Override
+ public String toString() {
+ return "ActorsFilms{" + "actor='" + actor + '\'' + ", movies=" + movies + '}';
+ }
+
+}
diff --git a/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/chat/MoonshotChatModelIT.java b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/chat/MoonshotChatModelIT.java
new file mode 100644
index 00000000000..f2e58b3ad62
--- /dev/null
+++ b/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/chat/MoonshotChatModelIT.java
@@ -0,0 +1,194 @@
+/*
+ * 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.moonshot.chat;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.ai.chat.messages.AssistantMessage;
+import org.springframework.ai.chat.messages.Message;
+import org.springframework.ai.chat.messages.UserMessage;
+import org.springframework.ai.chat.model.ChatModel;
+import org.springframework.ai.chat.model.ChatResponse;
+import org.springframework.ai.chat.model.Generation;
+import org.springframework.ai.chat.model.StreamingChatModel;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.chat.prompt.PromptTemplate;
+import org.springframework.ai.chat.prompt.SystemPromptTemplate;
+import org.springframework.ai.moonshot.MoonshotTestConfiguration;
+import org.springframework.ai.parser.BeanOutputParser;
+import org.springframework.ai.parser.ListOutputParser;
+import org.springframework.ai.parser.MapOutputParser;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.core.convert.support.DefaultConversionService;
+import org.springframework.core.io.Resource;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@SpringBootTest(classes = MoonshotTestConfiguration.class)
+@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".+")
+public class MoonshotChatModelIT {
+
+ private static final Logger logger = LoggerFactory.getLogger(MoonshotChatModelIT.class);
+
+ @Autowired
+ protected ChatModel chatModel;
+
+ @Autowired
+ protected StreamingChatModel streamingChatModel;
+
+ @Value("classpath:/prompts/system-message.st")
+ private Resource systemResource;
+
+ @Test
+ void roleTest() {
+ UserMessage userMessage = new UserMessage(
+ "Tell me about 3 famous pirates from the Golden Age of Piracy and why they did.");
+ SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
+ Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", "Bob", "voice", "pirate"));
+ Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
+ ChatResponse response = chatModel.call(prompt);
+ assertThat(response.getResults()).hasSize(1);
+ assertThat(response.getResults().get(0).getOutput().getContent()).contains("Blackbeard");
+ // needs fine tuning... evaluateQuestionAndAnswer(request, response, false);
+ }
+
+ @Test
+ void outputParser() {
+ DefaultConversionService conversionService = new DefaultConversionService();
+ ListOutputParser outputParser = new ListOutputParser(conversionService);
+
+ String format = outputParser.getFormat();
+ String template = """
+ List five {subject}
+ {format}
+ """;
+ PromptTemplate promptTemplate = new PromptTemplate(template,
+ Map.of("subject", "ice cream flavors", "format", format));
+ Prompt prompt = new Prompt(promptTemplate.createMessage());
+ Generation generation = this.chatModel.call(prompt).getResult();
+
+ List list = outputParser.parse(generation.getOutput().getContent());
+ assertThat(list).hasSize(5);
+
+ }
+
+ @Test
+ void mapOutputParser() {
+ MapOutputParser outputParser = new MapOutputParser();
+
+ String format = outputParser.getFormat();
+ String template = """
+ Provide me a List of {subject}
+ {format}
+ """;
+ PromptTemplate promptTemplate = new PromptTemplate(template,
+ Map.of("subject", "an array of numbers from 1 to 9 under they key name 'numbers'", "format", format));
+ Prompt prompt = new Prompt(promptTemplate.createMessage());
+ Generation generation = chatModel.call(prompt).getResult();
+
+ Map result = outputParser.parse(generation.getOutput().getContent());
+ assertThat(result.get("numbers")).isEqualTo(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
+
+ }
+
+ @Test
+ void beanOutputParser() {
+
+ BeanOutputParser outputParser = new BeanOutputParser<>(ActorsFilms.class);
+
+ String format = outputParser.getFormat();
+ String template = """
+ Generate the filmography for a random actor.
+ {format}
+
+ your response should be without ```json``` and $shcema.
+ """;
+ PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
+ Prompt prompt = new Prompt(promptTemplate.createMessage());
+ Generation generation = chatModel.call(prompt).getResult();
+
+ ActorsFilms actorsFilms = outputParser.parse(generation.getOutput().getContent());
+ }
+
+ record ActorsFilmsRecord(String actor, List movies) {
+ }
+
+ @Test
+ void beanOutputParserRecords() {
+
+ BeanOutputParser outputParser = new BeanOutputParser<>(ActorsFilmsRecord.class);
+
+ String format = outputParser.getFormat();
+ String template = """
+ Generate the filmography of 5 movies for Tom Hanks.
+ {format}
+
+ your response should be without ```json``` and $shcema.
+ """;
+ PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
+ Prompt prompt = new Prompt(promptTemplate.createMessage());
+ Generation generation = chatModel.call(prompt).getResult();
+
+ ActorsFilmsRecord actorsFilms = outputParser.parse(generation.getOutput().getContent());
+ logger.info("" + actorsFilms);
+ assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks");
+ assertThat(actorsFilms.movies()).hasSize(5);
+ }
+
+ @Test
+ void beanStreamOutputParserRecords() {
+
+ BeanOutputParser outputParser = new BeanOutputParser<>(ActorsFilmsRecord.class);
+
+ String format = outputParser.getFormat();
+ String template = """
+ Generate the filmography of 5 movies for Tom Hanks.
+ {format}
+
+ your response should be without ```json``` and $shcema.
+ """;
+ PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
+ Prompt prompt = new Prompt(promptTemplate.createMessage());
+
+ String generationTextFromStream = streamingChatModel.stream(prompt)
+ .collectList()
+ .block()
+ .stream()
+ .map(ChatResponse::getResults)
+ .flatMap(List::stream)
+ .map(Generation::getOutput)
+ .map(AssistantMessage::getContent)
+ .collect(Collectors.joining());
+
+ ActorsFilmsRecord actorsFilms = outputParser.parse(generationTextFromStream);
+ logger.info("" + actorsFilms);
+ assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks");
+ assertThat(actorsFilms.movies()).hasSize(5);
+ }
+
+}
diff --git a/models/spring-ai-moonshot/src/test/resources/prompts/system-message.st b/models/spring-ai-moonshot/src/test/resources/prompts/system-message.st
new file mode 100644
index 00000000000..579febd8d9b
--- /dev/null
+++ b/models/spring-ai-moonshot/src/test/resources/prompts/system-message.st
@@ -0,0 +1,3 @@
+You are an AI assistant that helps people find information.
+Your name is {name}.
+You should reply to the user's request with your name and also in the style of a {voice}.
\ No newline at end of file
diff --git a/pom.xml b/pom.xml
index 601e92da34c..e0e5e4d01a7 100644
--- a/pom.xml
+++ b/pom.xml
@@ -74,6 +74,7 @@
models/spring-ai-vertex-ai-palm2
models/spring-ai-watsonx-ai
models/spring-ai-zhipuai
+ models/spring-ai-moonshot
spring-ai-spring-boot-starters/spring-ai-starter-anthropic
spring-ai-spring-boot-starters/spring-ai-starter-azure-openai
spring-ai-spring-boot-starters/spring-ai-starter-bedrock-ai
@@ -89,6 +90,7 @@
spring-ai-spring-boot-starters/spring-ai-starter-vertex-ai-palm2
spring-ai-spring-boot-starters/spring-ai-starter-watsonx-ai
spring-ai-spring-boot-starters/spring-ai-starter-zhipuai
+ spring-ai-spring-boot-starters/spring-ai-starter-moonshot
vector-stores/spring-ai-opensearch-store
spring-ai-spring-boot-starters/spring-ai-starter-opensearch-store
diff --git a/spring-ai-bom/pom.xml b/spring-ai-bom/pom.xml
index b653d1e7e02..6b6c24b180a 100644
--- a/spring-ai-bom/pom.xml
+++ b/spring-ai-bom/pom.xml
@@ -130,6 +130,11 @@
${project.version}
+
+ org.springframework.ai
+ spring-ai-moonshot
+ ${project.version}
+
@@ -451,6 +456,12 @@
spring-ai-zhipuai-spring-boot-starter
${project.version}
+
+
+ org.springframework.ai
+ spring-ai-moonshot-spring-boot-starter
+ ${project.version}
+
diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc
index b554b8959cb..7bb124bd392 100644
--- a/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc
+++ b/spring-ai-docs/src/main/antora/modules/ROOT/nav.adoc
@@ -30,6 +30,8 @@
*** xref:api/chat/watsonx-ai-chat.adoc[Watsonx.AI]
*** xref:api/chat/minimax-chat.adoc[MiniMax]
**** xref:api/chat/functions/minimax-chat-functions.adoc[Function Calling]
+*** xref:api/chat/moonshot-chat.adoc[Moonshot AI]
+**** xref:api/chat/functions/moonshot-chat-functions.adoc[Function Calling]
** xref:api/embeddings.adoc[]
*** xref:api/embeddings/openai-embeddings.adoc[OpenAI]
*** xref:api/embeddings/ollama-embeddings.adoc[Ollama]
diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/functions/moonshot-chat-functions.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/functions/moonshot-chat-functions.adoc
new file mode 100644
index 00000000000..b82cdd62e1f
--- /dev/null
+++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/functions/moonshot-chat-functions.adoc
@@ -0,0 +1,226 @@
+= Function Calling
+
+You can register custom Java functions with the `MoonshotChatModel` and have the Moonshot model intelligently choose to output a JSON object containing arguments to call one or many of the registered functions.
+This allows you to connect the LLM capabilities with external tools and APIs.
+The Moonshot models are trained to detect when a function should be called and to respond with JSON that adheres to the function signature.
+
+The Moonshot API does not call the function directly; instead, the model generates JSON that you can use to call the function in your code and return the result back to the model to complete the conversation.
+
+Spring AI provides flexible and user-friendly ways to register and call custom functions.
+In general, the custom functions need to provide a function `name`, `description`, and the function call `signature` (as JSON schema) to let the model know what arguments the function expects. The `description` helps the model to understand when to call the function.
+
+As a developer, you need to implement a functions that takes the function call arguments sent from the AI model, and respond with the result back to the model. Your function can in turn invoke other 3rd party services to provide the results.
+
+Spring AI makes this as easy as defining a `@Bean` definition that returns a `java.util.Function` and supplying the bean name as an option when invoking the `ChatModel`.
+
+Under the hood, Spring wraps your POJO (the function) with the appropriate adapter code that enables interaction with the AI Model, saving you from writing tedious boilerplate code.
+The basis of the underlying infrastructure is the link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallback.java[FunctionCallback.java] interface and the companion link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/model/function/FunctionCallbackWrapper.java[FunctionCallbackWrapper.java] utility class to simplify the implementation and registration of Java callback functions.
+
+// Additionally, the Auto-Configuration provides a way to auto-register any Function beans definition as function calling candidates in the `ChatModel`.
+
+
+== How it works
+
+Suppose we want the AI model to respond with information that it does not have, for example the current temperature at a given location.
+
+We can provide the AI model with metadata about our own functions that it can use to retrieve that information as it processes your prompt.
+
+For example, if during the processing of a prompt, the AI Model determines that it needs additional information about the temperature in a given location, it will start a server side generated request/response interaction. The AI Model invokes a client side function.
+The AI Model provides method invocation details as JSON and it is the responsibility of the client to execute that function and return the response.
+
+The model-client interaction is illustrated in the <> diagram.
+
+Spring AI greatly simplifies code you need to write to support function invocation.
+It brokers the function invocation conversation for you.
+You can simply provide your function definition as a `@Bean` and then provide the bean name of the function in your prompt options.
+You can also reference multiple function bean names in your prompt.
+
+== Quick Start
+
+Let's create a chatbot that answer questions by calling our own function.
+To support the response of the chatbot, we will register our own function that takes a location and returns the current weather in that location.
+
+When the response to the prompt to the model needs to answer a question such as `"What’s the weather like in Boston?"` the AI model will invoke the client providing the location value as an argument to be passed to the function. This RPC-like data is passed as JSON.
+
+Our function calls some SaaS based weather service API and returns the weather response back to the model to complete the conversation. In this example we will use a simple implementation named `MockWeatherService` that hard codes the temperature for various locations.
+
+The following `MockWeatherService.java` represents the weather service API:
+
+[source,java]
+----
+public class MockWeatherService implements Function {
+
+ public enum Unit { C, F }
+ public record Request(String location, Unit unit) {}
+ public record Response(double temp, Unit unit) {}
+
+ public Response apply(Request request) {
+ return new Response(30.0, Unit.C);
+ }
+}
+----
+
+=== Registering Functions as Beans
+
+With the link:../minimax-chat.html#_auto_configuration[MoonshotChatModel Auto-Configuration] you have multiple ways to register custom functions as beans in the Spring context.
+
+We start with describing the most POJO friendly options.
+
+
+==== Plain Java Functions
+
+In this approach you define `@Beans` in your application context as you would any other Spring managed object.
+
+Internally, Spring AI `ChatModel` will create an instance of a `FunctionCallbackWrapper` wrapper that adds the logic for it being invoked via the AI model.
+The name of the `@Bean` is passed as a `ChatOption`.
+
+
+[source,java]
+----
+@Configuration
+static class Config {
+
+ @Bean
+ @Description("Get the weather in location") // function description
+ public Function weatherFunction1() {
+ return new MockWeatherService();
+ }
+ ...
+}
+----
+
+The `@Description` annotation is optional and provides a function description (2) that helps the model to understand when to call the function. It is an important property to set to help the AI model determine what client side function to invoke.
+
+Another option to provide the description of the function is to the `@JacksonDescription` annotation on the `MockWeatherService.Request` to provide the function description:
+
+[source,java]
+----
+
+@Configuration
+static class Config {
+
+ @Bean
+ public Function currentWeather3() { // (1) bean name as function name.
+ return new MockWeatherService();
+ }
+ ...
+}
+
+@JsonClassDescription("Get the weather in location") // (2) function description
+public record Request(String location, Unit unit) {}
+----
+
+It is a best practice to annotate the request object with information such that the generates JSON schema of that function is as descriptive as possible to help the AI model pick the correct function to invoke.
+
+The link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackWithPlainFunctionBeanIT.java[FunctionCallbackWithPlainFunctionBeanIT.java] demonstrates this approach.
+
+
+==== FunctionCallback Wrapper
+
+Another way register a function is to create `FunctionCallbackWrapper` wrapper like this:
+
+[source,java]
+----
+@Configuration
+static class Config {
+
+ @Bean
+ public FunctionCallback weatherFunctionInfo() {
+
+ return FunctionCallbackWrapper.builder(new MockWeatherService())
+ .withName("CurrentWeather") // (1) function name
+ .withDescription("Get the weather in location") // (2) function description
+ .build();
+ }
+ ...
+}
+----
+
+It wraps the 3rd party, `MockWeatherService` function and registers it as a `CurrentWeather` function with the `MoonshotChatModel`.
+It also provides a description (2) and an optional response converter (3) to convert the response into a text as expected by the model.
+
+NOTE: By default, the response converter does a JSON serialization of the Response object.
+
+NOTE: The `FunctionCallbackWrapper` internally resolves the function call signature based on the `MockWeatherService.Request` class.
+
+=== Specifying functions in Chat Options
+
+To let the model know and call your `CurrentWeather` function you need to enable it in your prompt requests:
+
+[source,java]
+----
+MoonshotChatModel chatModel = ...
+
+UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
+
+ChatResponse response = chatModel.call(new Prompt(List.of(userMessage),
+ MoonshotChatOptions.builder().withFunction("CurrentWeather").build())); // (1) Enable the function
+
+logger.info("Response: {}", response);
+----
+
+// NOTE: You can can have multiple functions registered in your `ChatModel` but only those enabled in the prompt request will be considered for the function calling.
+
+Above user question will trigger 3 calls to `CurrentWeather` function (one for each city) and the final response will be something like this:
+
+----
+Here is the current weather for the requested cities:
+- San Francisco, CA: 30.0°C
+- Tokyo, Japan: 10.0°C
+- Paris, France: 15.0°C
+----
+
+The link:https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackWrapperIT.java[FunctionCallbackWrapperIT.java] test demo this approach.
+
+
+=== Register/Call Functions with Prompt Options
+
+In addition to the auto-configuration you can register callback functions, dynamically, with your Prompt requests:
+
+[source,java]
+----
+MoonshotChatModel chatModel = ...
+
+UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
+
+var promptOptions = MoonshotChatOptions.builder()
+ .withFunctionCallbacks(List.of(new FunctionCallbackWrapper<>(
+ "CurrentWeather", // name
+ "Get the weather in location", // function description
+ new MockWeatherService()))) // function code
+ .build();
+
+ChatResponse response = chatModel.call(new Prompt(List.of(userMessage), promptOptions));
+----
+
+NOTE: The in-prompt registered functions are enabled by default for the duration of this request.
+
+This approach allows to dynamically chose different functions to be called based on the user input.
+
+The https://github.com/spring-projects/spring-ai/blob/main/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackInPromptIT.java[FunctionCallbackInPromptIT.java] integration test provides a complete example of how to register a function with the `MoonshotChatModel` and use it in a prompt request.
+//
+// === Register Functions with Default Options
+//
+// You can programmatically register functions with the `MoonshotChatModel` using the `MoonshotChatOptions#withFunctionCallbacks`:
+//
+// [source,java]
+// ----
+//
+// MoonshotApi moonshotApi = new MoonshotApi(apiKey);
+//
+// var defaultOptions = MoonshotChatOptions.builder()
+// .withFunctionCallbacks(List.of(new FunctionCallbackWrapper<>(
+// "CurrentWeather", // name
+// "Get the weather in location", // function description
+// new MockWeatherService()))) // function code
+// .build();
+//
+// MoonshotChatModel chatModel = new MoonshotChatModel(moonshotApi, defaultOptions);
+//
+// UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
+//
+// ChatResponse response = chatModel.call(new Prompt(List.of(userMessage),
+// MoonshotChatOptions.builder().withFunction("CurrentWeather").build())); // Enable the function
+// ----
+//
+// NOTE: Functions are registered when MoonshotChatModel is created, by you must enable in the Prompt the functions to be used in the request.
diff --git a/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/moonshot-chat.adoc b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/moonshot-chat.adoc
new file mode 100644
index 00000000000..284b6a2521e
--- /dev/null
+++ b/spring-ai-docs/src/main/antora/modules/ROOT/pages/api/chat/moonshot-chat.adoc
@@ -0,0 +1,250 @@
+= Moonshot AI Chat
+
+Spring AI supports the various AI language models from Moonshot AI. You can interact with Moonshot AI language models and create a multilingual conversational assistant based on Moonshot models.
+
+== Prerequisites
+
+You will need to create an API with Moonshot to access Moonshot AI language models.
+Create an account at https://platform.moonshot.cn/console[Moonshot AI registration page] and generate the token on the https://platform.moonshot.cn/console/api-keys/[API Keys page].
+The Spring AI project defines a configuration property named `spring.ai.moonshot.api-key` that you should set to the value of the `API Key` obtained from https://platform.moonshot.cn/console/api-keys/[API Keys page].
+Exporting an environment variable is one way to set that configuration property:
+
+[source,shell]
+----
+export SPRING_AI_MOONSHOT_API_KEY=
+----
+
+=== Add Repositories and BOM
+
+Spring AI artifacts are published in Spring Milestone and Snapshot repositories.
+Refer to the xref:getting-started.adoc#repositories[Repositories] section to add these repositories to your build system.
+
+To help with dependency management, Spring AI provides a BOM (bill of materials) to ensure that a consistent version of Spring AI is used throughout the entire project. Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build system.
+
+
+
+== Auto-configuration
+
+Spring AI provides Spring Boot auto-configuration for the Moonshot Chat Model.
+To enable it add the following dependency to your project's Maven `pom.xml` file:
+
+[source, xml]
+----
+
+ org.springframework.ai
+ spring-ai-moonshot-spring-boot-starter
+
+----
+
+or to your Gradle `build.gradle` build file.
+
+[source,groovy]
+----
+dependencies {
+ implementation 'org.springframework.ai:spring-ai-moonshot-spring-boot-starter'
+}
+----
+
+TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
+
+=== Chat Properties
+
+==== Retry Properties
+
+The prefix `spring.ai.retry` is used as the property prefix that lets you configure the retry mechanism for the Moonshot AI Chat model.
+
+[cols="3,5,1"]
+|====
+| Property | Description | Default
+
+| spring.ai.retry.max-attempts | Maximum number of retry attempts. | 10
+| spring.ai.retry.backoff.initial-interval | Initial sleep duration for the exponential backoff policy. | 2 sec.
+| spring.ai.retry.backoff.multiplier | Backoff interval multiplier. | 5
+| spring.ai.retry.backoff.max-interval | Maximum backoff duration. | 3 min.
+| spring.ai.retry.on-client-errors | If false, throw a NonTransientAiException, and do not attempt retry for `4xx` client error codes | false
+| spring.ai.retry.exclude-on-http-codes | List of HTTP status codes that should not trigger a retry (e.g. to throw NonTransientAiException). | empty
+| spring.ai.retry.on-http-codes | List of HTTP status codes that should trigger a retry (e.g. to throw TransientAiException). | empty
+|====
+
+==== Connection Properties
+
+The prefix `spring.ai.moonshot` is used as the property prefix that lets you connect to Moonshot.
+
+[cols="3,5,1"]
+|====
+| Property | Description | Default
+
+| spring.ai.moonshot.base-url | The URL to connect to | https://api.moonshot.cn
+| spring.ai.moonshot.api-key | The API Key | -
+|====
+
+==== Configuration Properties
+
+The prefix `spring.ai.moonshot.chat` is the property prefix that lets you configure the chat model implementation for Moonshot.
+
+[cols="3,5,1"]
+|====
+| Property | Description | Default
+
+| spring.ai.moonshot.chat.enabled | Enable Moonshot chat model. | true
+| spring.ai.moonshot.chat.base-url | Optional overrides the spring.ai.moonshot.base-url to provide chat specific url | -
+| spring.ai.moonshot.chat.api-key | Optional overrides the spring.ai.moonshot.api-key to provide chat specific api-key | -
+| spring.ai.moonshot.chat.options.model | This is the Moonshot Chat model to use | `moonshot-v1-8k` (the `moonshot-v1-8k`, `moonshot-v1-32k`, and `moonshot-v1-128k` point to the latest model versions)
+| spring.ai.moonshot.chat.options.maxTokens | The maximum number of tokens to generate in the chat completion. The total length of input tokens and generated tokens is limited by the model's context length. | -
+| spring.ai.moonshot.chat.options.temperature | The sampling temperature to use that controls the apparent creativity of generated completions. Higher values will make output more random while lower values will make results more focused and deterministic. It is not recommended to modify temperature and top_p for the same completions request as the interaction of these two settings is difficult to predict. | 0.7
+| spring.ai.moonshot.chat.options.topP | An alternative to sampling with temperature, called nucleus sampling, where the model considers the results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10% probability mass are considered. We generally recommend altering this or temperature but not both. | 1.0
+| spring.ai.moonshot.chat.options.n | How many chat completion choices to generate for each input message. Note that you will be charged based on the number of generated tokens across all of the choices. Default value is 1 and cannot be greater than 5. Specifically, when the temperature is very small and close to 0, we can only return 1 result. If n is already set and>1 at this time, service will return an illegal input parameter (invalid_request_error) | 1
+| spring.ai.moonshot.chat.options.presencePenalty | Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far, increasing the model's likelihood to talk about new topics. | 0.0f
+| spring.ai.moonshot.chat.options.frequencyPenalty | Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far, decreasing the model's likelihood to repeat the same line verbatim. | 0.0f
+| spring.ai.moonshot.chat.options.stop | Up to 5 sequences where the API will stop generating further tokens. Each string must not exceed 32 bytes | -
+|====
+
+NOTE: You can override the common `spring.ai.moonshot.base-url` and `spring.ai.moonshot.api-key` for the `ChatModel` implementations.
+The `spring.ai.moonshot.chat.base-url` and `spring.ai.moonshot.chat.api-key` properties if set take precedence over the common properties.
+This is useful if you want to use different Moonshot accounts for different models and different model endpoints.
+
+TIP: All properties prefixed with `spring.ai.moonshot.chat.options` can be overridden at runtime by adding a request specific <> to the `Prompt` call.
+
+== Runtime Options [[chat-options]]
+
+The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/MoonshotChatOptions.java[MoonshotChatOptions.java] provides model configurations, such as the model to use, the temperature, the frequency penalty, etc.
+
+On start-up, the default options can be configured with the `MoonshotChatModel(api, options)` constructor or the `spring.ai.moonshot.chat.options.*` properties.
+
+At run-time you can override the default options by adding new, request specific, options to the `Prompt` call.
+For example to override the default model and temperature for a specific request:
+
+[source,java]
+----
+ChatResponse response = chatModel.call(
+ new Prompt(
+ "Generate the names of 5 famous pirates.",
+ MoonshotChatOptions.builder()
+ .withModel(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue())
+ .withTemperature(0.5f)
+ .build()
+ ));
+----
+
+TIP: In addition to the model specific link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/MoonshotChatOptions.java[MoonshotChatOptions] you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/ChatOptions.java[ChatOptions] instance, created with the https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/ChatOptionsBuilder.java[ChatOptionsBuilder#builder()].
+
+== Sample Controller (Auto-configuration)
+
+https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-moonshot-spring-boot-starter` to your pom (or gradle) dependencies.
+
+Add a `application.properties` file, under the `src/main/resources` directory, to enable and configure the Moonshot Chat model:
+
+[source,application.properties]
+----
+spring.ai.moonshot.api-key=YOUR_API_KEY
+spring.ai.moonshot.chat.options.model=moonshot-v1-8k
+spring.ai.moonshot.chat.options.temperature=0.7
+----
+
+TIP: replace the `api-key` with your Moonshot credentials.
+
+This will create a `MoonshotChatModel` implementation that you can inject into your class.
+Here is an example of a simple `@Controller` class that uses the chat model for text generations.
+
+[source,java]
+----
+@RestController
+public class ChatController {
+
+ private final MoonshotChatModel chatModel;
+
+ @Autowired
+ public ChatController(MoonshotChatModel chatModel) {
+ this.chatModel = chatModel;
+ }
+
+ @GetMapping("/ai/generate")
+ public Map generate(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
+ return Map.of("generation", chatModel.call(message));
+ }
+
+ @GetMapping("/ai/generateStream")
+ public Flux generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
+ var prompt = new Prompt(new UserMessage(message));
+ return chatModel.stream(prompt);
+ }
+}
+----
+
+== Manual Configuration
+
+The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/MoonshotChatModel.java[MoonshotChatModel] implements the `ChatModel` and `StreamingChatModel` and uses the <> to connect to the Moonshot service.
+
+Add the `spring-ai-moonshot` dependency to your project's Maven `pom.xml` file:
+
+[source, xml]
+----
+
+ org.springframework.ai
+ spring-ai-moonshot
+
+----
+
+or to your Gradle `build.gradle` build file.
+
+[source,groovy]
+----
+dependencies {
+ implementation 'org.springframework.ai:spring-ai-moonshot'
+}
+----
+
+TIP: Refer to the xref:getting-started.adoc#dependency-management[Dependency Management] section to add the Spring AI BOM to your build file.
+
+Next, create a `MoonshotChatModel` and use it for text generations:
+
+[source,java]
+----
+var moonshotApi = new MoonshotApi(System.getenv("MOONSHOT_API_KEY"));
+
+var chatModel = new MoonshotChatModel(moonshotApi, MoonshotChatOptions.builder()
+ .withModel(MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue())
+ .withTemperature(0.4f)
+ .withMaxTokens(200)
+ .build());
+
+ChatResponse response = chatModel.call(
+ new Prompt("Generate the names of 5 famous pirates."));
+
+// Or with streaming responses
+Flux streamResponse = chatModel.stream(
+ new Prompt("Generate the names of 5 famous pirates."));
+----
+
+The `MoonshotChatOptions` provides the configuration information for the chat requests.
+The `MoonshotChatOptions.Builder` is fluent options builder.
+
+=== Low-level Moonshot Api Client [[low-level-api]]
+
+The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/api/MoonshotApi.java[MoonshotApi] provides is lightweight Java client for link:https://platform.moonshot.cn/docs/api-reference[Moonshot AI API].
+
+Here is a simple snippet how to use the api programmatically:
+
+[source,java]
+----
+MoonshotApi moonshotApi =
+ new MoonshotApi(System.getenv("MOONSHOT_API_KEY"));
+
+ChatCompletionMessage chatCompletionMessage =
+ new ChatCompletionMessage("Hello world", Role.USER);
+
+// Sync request
+ResponseEntity response = moonshotApi.chatCompletionEntity(
+ new ChatCompletionRequest(List.of(chatCompletionMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.7f, false));
+
+// Streaming request
+Flux streamResponse = moonshotApi.chatCompletionStream(
+ new ChatCompletionRequest(List.of(chatCompletionMessage), MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue(), 0.7f, true));
+----
+
+Follow the https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-moonshot/src/main/java/org/springframework/ai/moonshot/api/MoonshotApi.java[MoonshotApi.java]'s JavaDoc for further information.
+
+==== MoonshotApi Samples
+* The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MoonshotApiIT.java[MoonshotApiIT.java] test provides some general examples how to use the lightweight library.
+
+* The link:https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-moonshot/src/test/java/org/springframework/ai/moonshot/api/MoonshotApiToolFunctionCallIT.java.java[MoonshotApiToolFunctionCallIT.java] test shows how to use the low-level API to call tool functions.
\ No newline at end of file
diff --git a/spring-ai-spring-boot-autoconfigure/pom.xml b/spring-ai-spring-boot-autoconfigure/pom.xml
index 291129335cb..81276ea0759 100644
--- a/spring-ai-spring-boot-autoconfigure/pom.xml
+++ b/spring-ai-spring-boot-autoconfigure/pom.xml
@@ -311,6 +311,14 @@
true
+
+
+ org.springframework.ai
+ spring-ai-moonshot
+ ${project.parent.version}
+ true
+
+
org.springframework.ai
diff --git a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotAutoConfiguration.java b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotAutoConfiguration.java
new file mode 100644
index 00000000000..8c2f019542f
--- /dev/null
+++ b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotAutoConfiguration.java
@@ -0,0 +1,68 @@
+/*
+ * 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.autoconfigure.moonshot;
+
+import org.springframework.ai.autoconfigure.retry.SpringAiRetryAutoConfiguration;
+import org.springframework.ai.moonshot.MoonshotChatModel;
+import org.springframework.ai.moonshot.api.MoonshotApi;
+import org.springframework.boot.autoconfigure.AutoConfiguration;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
+import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.context.annotation.Bean;
+import org.springframework.retry.support.RetryTemplate;
+import org.springframework.util.Assert;
+import org.springframework.util.StringUtils;
+import org.springframework.web.client.ResponseErrorHandler;
+import org.springframework.web.client.RestClient;
+
+/**
+ * @author Geng Rong
+ */
+@AutoConfiguration(after = { RestClientAutoConfiguration.class, SpringAiRetryAutoConfiguration.class })
+@EnableConfigurationProperties({ MoonshotCommonProperties.class, MoonshotChatProperties.class })
+@ConditionalOnClass(MoonshotApi.class)
+public class MoonshotAutoConfiguration {
+
+ @Bean
+ @ConditionalOnMissingBean
+ @ConditionalOnProperty(prefix = MoonshotChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
+ matchIfMissing = true)
+ public MoonshotChatModel moonshotChatModel(MoonshotCommonProperties commonProperties,
+ MoonshotChatProperties chatProperties, RestClient.Builder restClientBuilder, RetryTemplate retryTemplate,
+ ResponseErrorHandler responseErrorHandler) {
+
+ var moonshotApi = moonshotApi(chatProperties.getApiKey(), commonProperties.getApiKey(),
+ chatProperties.getBaseUrl(), commonProperties.getBaseUrl(), restClientBuilder, responseErrorHandler);
+
+ return new MoonshotChatModel(moonshotApi, chatProperties.getOptions(), retryTemplate);
+ }
+
+ private MoonshotApi moonshotApi(String apiKey, String commonApiKey, String baseUrl, String commonBaseUrl,
+ RestClient.Builder restClientBuilder, ResponseErrorHandler responseErrorHandler) {
+
+ var resolvedApiKey = StringUtils.hasText(apiKey) ? apiKey : commonApiKey;
+ var resoledBaseUrl = StringUtils.hasText(baseUrl) ? baseUrl : commonBaseUrl;
+
+ Assert.hasText(resolvedApiKey, "Moonshot API key must be set");
+ Assert.hasText(resoledBaseUrl, "Moonshot base URL must be set");
+
+ return new MoonshotApi(resoledBaseUrl, resolvedApiKey, restClientBuilder, responseErrorHandler);
+ }
+
+}
diff --git a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotChatProperties.java b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotChatProperties.java
new file mode 100644
index 00000000000..843a2642472
--- /dev/null
+++ b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotChatProperties.java
@@ -0,0 +1,62 @@
+/*
+ * 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.autoconfigure.moonshot;
+
+import org.springframework.ai.moonshot.MoonshotChatOptions;
+import org.springframework.ai.moonshot.api.MoonshotApi;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.boot.context.properties.NestedConfigurationProperty;
+
+/**
+ * @author Geng Rong
+ */
+@ConfigurationProperties(MoonshotChatProperties.CONFIG_PREFIX)
+public class MoonshotChatProperties extends MoonshotParentProperties {
+
+ public static final String CONFIG_PREFIX = "spring.ai.moonshot.chat";
+
+ public static final String DEFAULT_CHAT_MODEL = MoonshotApi.ChatModel.MOONSHOT_V1_8K.getValue();
+
+ private static final Double DEFAULT_TEMPERATURE = 0.7;
+
+ /**
+ * Enable Moonshot chat client.
+ */
+ private boolean enabled = true;
+
+ @NestedConfigurationProperty
+ private MoonshotChatOptions options = MoonshotChatOptions.builder()
+ .withModel(DEFAULT_CHAT_MODEL)
+ .withTemperature(DEFAULT_TEMPERATURE.floatValue())
+ .build();
+
+ public MoonshotChatOptions getOptions() {
+ return this.options;
+ }
+
+ public void setOptions(MoonshotChatOptions options) {
+ this.options = options;
+ }
+
+ public boolean isEnabled() {
+ return this.enabled;
+ }
+
+ public void setEnabled(boolean enabled) {
+ this.enabled = enabled;
+ }
+
+}
diff --git a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotCommonProperties.java b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotCommonProperties.java
new file mode 100644
index 00000000000..07525a3113a
--- /dev/null
+++ b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotCommonProperties.java
@@ -0,0 +1,34 @@
+/*
+ * 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.autoconfigure.moonshot;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+
+/**
+ * @author Geng Rong
+ */
+@ConfigurationProperties(MoonshotCommonProperties.CONFIG_PREFIX)
+public class MoonshotCommonProperties extends MoonshotParentProperties {
+
+ public static final String CONFIG_PREFIX = "spring.ai.moonshot";
+
+ public static final String DEFAULT_BASE_URL = "https://api.moonshot.cn";
+
+ public MoonshotCommonProperties() {
+ super.setBaseUrl(DEFAULT_BASE_URL);
+ }
+
+}
diff --git a/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotParentProperties.java b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotParentProperties.java
new file mode 100644
index 00000000000..54f3f5f3b54
--- /dev/null
+++ b/spring-ai-spring-boot-autoconfigure/src/main/java/org/springframework/ai/autoconfigure/moonshot/MoonshotParentProperties.java
@@ -0,0 +1,43 @@
+/*
+ * 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.autoconfigure.moonshot;
+
+/**
+ * @author Geng Rong
+ */
+public class MoonshotParentProperties {
+
+ private String apiKey;
+
+ private String baseUrl;
+
+ public String getApiKey() {
+ return this.apiKey;
+ }
+
+ public void setApiKey(String apiKey) {
+ this.apiKey = apiKey;
+ }
+
+ public String getBaseUrl() {
+ return this.baseUrl;
+ }
+
+ public void setBaseUrl(String baseUrl) {
+ this.baseUrl = baseUrl;
+ }
+
+}
diff --git a/spring-ai-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports b/spring-ai-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
index 5117fd476e9..afda4c7cda4 100644
--- a/spring-ai-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
+++ b/spring-ai-spring-boot-autoconfigure/src/main/resources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
@@ -37,3 +37,4 @@ org.springframework.ai.autoconfigure.zhipuai.ZhiPuAiAutoConfiguration
org.springframework.ai.autoconfigure.chat.client.ChatClientAutoConfiguration
org.springframework.ai.autoconfigure.vectorstore.typesense.TypesenseVectorStoreAutoConfiguration
org.springframework.ai.autoconfigure.vectorstore.opensearch.OpenSearchVectorStoreAutoConfiguration
+org.springframework.ai.autoconfigure.moonshot.MoonshotAutoConfiguration
diff --git a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/MoonshotAutoConfigurationIT.java b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/MoonshotAutoConfigurationIT.java
new file mode 100644
index 00000000000..da456b60eef
--- /dev/null
+++ b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/MoonshotAutoConfigurationIT.java
@@ -0,0 +1,75 @@
+/*
+ * 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.autoconfigure.moonshot;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.springframework.ai.autoconfigure.retry.SpringAiRetryAutoConfiguration;
+import org.springframework.ai.chat.model.ChatResponse;
+import org.springframework.ai.chat.messages.UserMessage;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.moonshot.MoonshotChatModel;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+import reactor.core.publisher.Flux;
+
+import java.util.Objects;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".*")
+public class MoonshotAutoConfigurationIT {
+
+ private static final Log logger = LogFactory.getLog(MoonshotAutoConfigurationIT.class);
+
+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
+ .withPropertyValues("spring.ai.moonshot.apiKey=" + System.getenv("MOONSHOT_API_KEY"))
+ .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
+ RestClientAutoConfiguration.class, MoonshotAutoConfiguration.class));
+
+ @Test
+ void generate() {
+ contextRunner.run(context -> {
+ MoonshotChatModel client = context.getBean(MoonshotChatModel.class);
+ String response = client.call("Hello");
+ assertThat(response).isNotEmpty();
+ logger.info("Response: " + response);
+ });
+ }
+
+ @Test
+ void generateStreaming() {
+ contextRunner.run(context -> {
+ MoonshotChatModel client = context.getBean(MoonshotChatModel.class);
+ Flux responseFlux = client.stream(new Prompt(new UserMessage("Hello")));
+ String response = Objects.requireNonNull(responseFlux.collectList().block())
+ .stream()
+ .map(chatResponse -> chatResponse.getResults().get(0).getOutput().getContent())
+ .collect(Collectors.joining());
+
+ assertThat(response).isNotEmpty();
+ logger.info("Response: " + response);
+ });
+ }
+
+}
diff --git a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/MoonshotPropertiesTests.java b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/MoonshotPropertiesTests.java
new file mode 100644
index 00000000000..1ee7986b815
--- /dev/null
+++ b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/MoonshotPropertiesTests.java
@@ -0,0 +1,164 @@
+/*
+ * 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.autoconfigure.moonshot;
+
+import org.junit.jupiter.api.Test;
+import org.springframework.ai.autoconfigure.retry.SpringAiRetryAutoConfiguration;
+import org.springframework.ai.moonshot.MoonshotChatModel;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+public class MoonshotPropertiesTests {
+
+ @Test
+ public void chatProperties() {
+
+ new ApplicationContextRunner().withPropertyValues(
+ // @formatter:off
+ "spring.ai.moonshot.base-url=TEST_BASE_URL",
+ "spring.ai.moonshot.api-key=abc123",
+ "spring.ai.moonshot.chat.options.model=MODEL_XYZ",
+ "spring.ai.moonshot.chat.options.temperature=0.55")
+ // @formatter:on
+ .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
+ RestClientAutoConfiguration.class, MoonshotAutoConfiguration.class))
+ .run(context -> {
+ var chatProperties = context.getBean(MoonshotChatProperties.class);
+ var connectionProperties = context.getBean(MoonshotCommonProperties.class);
+
+ assertThat(connectionProperties.getApiKey()).isEqualTo("abc123");
+ assertThat(connectionProperties.getBaseUrl()).isEqualTo("TEST_BASE_URL");
+
+ assertThat(chatProperties.getApiKey()).isNull();
+ assertThat(chatProperties.getBaseUrl()).isNull();
+
+ assertThat(chatProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ");
+ assertThat(chatProperties.getOptions().getTemperature()).isEqualTo(0.55f);
+ });
+ }
+
+ @Test
+ public void chatOverrideConnectionProperties() {
+
+ new ApplicationContextRunner().withPropertyValues(
+ // @formatter:off
+ "spring.ai.moonshot.base-url=TEST_BASE_URL",
+ "spring.ai.moonshot.api-key=abc123",
+ "spring.ai.moonshot.chat.base-url=TEST_BASE_URL2",
+ "spring.ai.moonshot.chat.api-key=456",
+ "spring.ai.moonshot.chat.options.model=MODEL_XYZ",
+ "spring.ai.moonshot.chat.options.temperature=0.55")
+ // @formatter:on
+ .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
+ RestClientAutoConfiguration.class, MoonshotAutoConfiguration.class))
+ .run(context -> {
+ var chatProperties = context.getBean(MoonshotChatProperties.class);
+ var connectionProperties = context.getBean(MoonshotCommonProperties.class);
+
+ assertThat(connectionProperties.getApiKey()).isEqualTo("abc123");
+ assertThat(connectionProperties.getBaseUrl()).isEqualTo("TEST_BASE_URL");
+
+ assertThat(chatProperties.getApiKey()).isEqualTo("456");
+ assertThat(chatProperties.getBaseUrl()).isEqualTo("TEST_BASE_URL2");
+
+ assertThat(chatProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ");
+ assertThat(chatProperties.getOptions().getTemperature()).isEqualTo(0.55f);
+ });
+ }
+
+ @Test
+ public void chatOptionsTest() {
+
+ new ApplicationContextRunner().withPropertyValues(
+ // @formatter:off
+ "spring.ai.moonshot.api-key=API_KEY",
+ "spring.ai.moonshot.base-url=TEST_BASE_URL",
+
+ "spring.ai.moonshot.chat.options.model=MODEL_XYZ",
+ "spring.ai.moonshot.chat.options.frequencyPenalty=-1.5",
+ "spring.ai.moonshot.chat.options.logitBias.myTokenId=-5",
+ "spring.ai.moonshot.chat.options.maxTokens=123",
+ "spring.ai.moonshot.chat.options.n=10",
+ "spring.ai.moonshot.chat.options.presencePenalty=0",
+ "spring.ai.moonshot.chat.options.responseFormat.type=json",
+ "spring.ai.moonshot.chat.options.seed=66",
+ "spring.ai.moonshot.chat.options.stop=boza,koza",
+ "spring.ai.moonshot.chat.options.temperature=0.55",
+ "spring.ai.moonshot.chat.options.topP=0.56",
+ "spring.ai.moonshot.chat.options.user=userXYZ"
+ )
+ // @formatter:on
+ .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
+ RestClientAutoConfiguration.class, MoonshotAutoConfiguration.class))
+ .run(context -> {
+ var chatProperties = context.getBean(MoonshotChatProperties.class);
+ var connectionProperties = context.getBean(MoonshotCommonProperties.class);
+
+ assertThat(connectionProperties.getBaseUrl()).isEqualTo("TEST_BASE_URL");
+ assertThat(connectionProperties.getApiKey()).isEqualTo("API_KEY");
+
+ assertThat(chatProperties.getOptions().getModel()).isEqualTo("MODEL_XYZ");
+ assertThat(chatProperties.getOptions().getFrequencyPenalty()).isEqualTo(-1.5f);
+ assertThat(chatProperties.getOptions().getMaxTokens()).isEqualTo(123);
+ assertThat(chatProperties.getOptions().getN()).isEqualTo(10);
+ assertThat(chatProperties.getOptions().getPresencePenalty()).isEqualTo(0);
+ assertThat(chatProperties.getOptions().getStop()).contains("boza", "koza");
+ assertThat(chatProperties.getOptions().getTemperature()).isEqualTo(0.55f);
+ assertThat(chatProperties.getOptions().getTopP()).isEqualTo(0.56f);
+
+ assertThat(chatProperties.getOptions().getUser()).isEqualTo("userXYZ");
+ });
+ }
+
+ @Test
+ void chatActivation() {
+ new ApplicationContextRunner()
+ .withPropertyValues("spring.ai.moonshot.api-key=API_KEY", "spring.ai.moonshot.base-url=TEST_BASE_URL",
+ "spring.ai.moonshot.chat.enabled=false")
+ .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
+ RestClientAutoConfiguration.class, MoonshotAutoConfiguration.class))
+ .run(context -> {
+ assertThat(context.getBeansOfType(MoonshotChatProperties.class)).isNotEmpty();
+ assertThat(context.getBeansOfType(MoonshotChatModel.class)).isEmpty();
+ });
+
+ new ApplicationContextRunner()
+ .withPropertyValues("spring.ai.moonshot.api-key=API_KEY", "spring.ai.moonshot.base-url=TEST_BASE_URL")
+ .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
+ RestClientAutoConfiguration.class, MoonshotAutoConfiguration.class))
+ .run(context -> {
+ assertThat(context.getBeansOfType(MoonshotChatProperties.class)).isNotEmpty();
+ assertThat(context.getBeansOfType(MoonshotChatModel.class)).isNotEmpty();
+ });
+
+ new ApplicationContextRunner()
+ .withPropertyValues("spring.ai.moonshot.api-key=API_KEY", "spring.ai.moonshot.base-url=TEST_BASE_URL",
+ "spring.ai.moonshot.chat.enabled=true")
+ .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
+ RestClientAutoConfiguration.class, MoonshotAutoConfiguration.class))
+ .run(context -> {
+ assertThat(context.getBeansOfType(MoonshotChatProperties.class)).isNotEmpty();
+ assertThat(context.getBeansOfType(MoonshotChatModel.class)).isNotEmpty();
+ });
+ }
+
+}
diff --git a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackInPromptIT.java b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackInPromptIT.java
new file mode 100644
index 00000000000..159d8a79355
--- /dev/null
+++ b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackInPromptIT.java
@@ -0,0 +1,114 @@
+/*
+ * 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.autoconfigure.moonshot.tool;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.ai.autoconfigure.moonshot.MoonshotAutoConfiguration;
+import org.springframework.ai.autoconfigure.retry.SpringAiRetryAutoConfiguration;
+import org.springframework.ai.chat.messages.AssistantMessage;
+import org.springframework.ai.chat.messages.UserMessage;
+import org.springframework.ai.chat.model.ChatResponse;
+import org.springframework.ai.chat.model.Generation;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.model.function.FunctionCallbackWrapper;
+import org.springframework.ai.moonshot.MoonshotChatModel;
+import org.springframework.ai.moonshot.MoonshotChatOptions;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+import reactor.core.publisher.Flux;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".*")
+public class FunctionCallbackInPromptIT {
+
+ private final Logger logger = LoggerFactory.getLogger(FunctionCallbackInPromptIT.class);
+
+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
+ .withPropertyValues("spring.ai.moonshot.apiKey=" + System.getenv("MOONSHOT_API_KEY"))
+ .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
+ RestClientAutoConfiguration.class, MoonshotAutoConfiguration.class));
+
+ @Test
+ void functionCallTest() {
+ contextRunner.withPropertyValues("spring.ai.moonshot.chat.options.model=abab6-chat").run(context -> {
+
+ MoonshotChatModel chatModel = context.getBean(MoonshotChatModel.class);
+
+ UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
+
+ var promptOptions = MoonshotChatOptions.builder()
+ .withFunctionCallbacks(List.of(FunctionCallbackWrapper.builder(new MockWeatherService())
+ .withName("CurrentWeatherService")
+ .withDescription("Get the weather in location")
+ .withResponseConverter((response) -> "" + response.temp() + response.unit())
+ .build()))
+ .build();
+
+ ChatResponse response = chatModel.call(new Prompt(List.of(userMessage), promptOptions));
+
+ logger.info("Response: {}", response);
+
+ assertThat(response.getResult().getOutput().getContent()).contains("30", "10", "15");
+ });
+ }
+
+ @Test
+ void streamingFunctionCallTest() {
+
+ contextRunner.withPropertyValues("spring.ai.moonshot.chat.options.model=abab6-chat").run(context -> {
+
+ MoonshotChatModel chatModel = context.getBean(MoonshotChatModel.class);
+
+ UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
+
+ var promptOptions = MoonshotChatOptions.builder()
+ .withFunctionCallbacks(List.of(FunctionCallbackWrapper.builder(new MockWeatherService())
+ .withName("CurrentWeatherService")
+ .withDescription("Get the weather in location")
+ .withResponseConverter((response) -> "" + response.temp() + response.unit())
+ .build()))
+ .build();
+
+ Flux response = chatModel.stream(new Prompt(List.of(userMessage), promptOptions));
+
+ String content = response.collectList()
+ .block()
+ .stream()
+ .map(ChatResponse::getResults)
+ .flatMap(List::stream)
+ .map(Generation::getOutput)
+ .map(AssistantMessage::getContent)
+ .collect(Collectors.joining());
+ logger.info("Response: {}", content);
+
+ assertThat(content).containsAnyOf("30.0", "30");
+ assertThat(content).containsAnyOf("10.0", "10");
+ assertThat(content).containsAnyOf("15.0", "15");
+ });
+ }
+
+}
\ No newline at end of file
diff --git a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackWithPlainFunctionBeanIT.java b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackWithPlainFunctionBeanIT.java
new file mode 100644
index 00000000000..35eb8704884
--- /dev/null
+++ b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackWithPlainFunctionBeanIT.java
@@ -0,0 +1,172 @@
+/*
+ * 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.autoconfigure.moonshot.tool;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.ai.autoconfigure.moonshot.MoonshotAutoConfiguration;
+import org.springframework.ai.autoconfigure.retry.SpringAiRetryAutoConfiguration;
+import org.springframework.ai.chat.messages.AssistantMessage;
+import org.springframework.ai.chat.messages.UserMessage;
+import org.springframework.ai.chat.model.ChatResponse;
+import org.springframework.ai.chat.model.Generation;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.model.function.FunctionCallingOptions;
+import org.springframework.ai.model.function.FunctionCallingOptionsBuilder.PortableFunctionCallingOptions;
+import org.springframework.ai.moonshot.MoonshotChatModel;
+import org.springframework.ai.moonshot.MoonshotChatOptions;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.Description;
+import reactor.core.publisher.Flux;
+
+import java.util.List;
+import java.util.function.Function;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".*")
+class FunctionCallbackWithPlainFunctionBeanIT {
+
+ private final Logger logger = LoggerFactory.getLogger(FunctionCallbackWithPlainFunctionBeanIT.class);
+
+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
+ .withPropertyValues("spring.ai.moonshot.apiKey=" + System.getenv("MOONSHOT_API_KEY"))
+ .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
+ RestClientAutoConfiguration.class, MoonshotAutoConfiguration.class))
+ .withUserConfiguration(Config.class);
+
+ @Test
+ void functionCallTest() {
+ contextRunner.withPropertyValues("spring.ai.moonshot.chat.options.model=abab6-chat").run(context -> {
+
+ MoonshotChatModel chatModel = context.getBean(MoonshotChatModel.class);
+
+ // Test weatherFunction
+ UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
+
+ ChatResponse response = chatModel.call(new Prompt(List.of(userMessage),
+ MoonshotChatOptions.builder().withFunction("weatherFunction").build()));
+
+ logger.info("Response: {}", response);
+
+ assertThat(response.getResult().getOutput().getContent()).contains("30", "10", "15");
+
+ // Test weatherFunctionTwo
+ response = chatModel.call(new Prompt(List.of(userMessage),
+ MoonshotChatOptions.builder().withFunction("weatherFunctionTwo").build()));
+
+ logger.info("Response: {}", response);
+
+ assertThat(response.getResult().getOutput().getContent()).contains("30", "10", "15");
+
+ });
+ }
+
+ @Test
+ void functionCallWithPortableFunctionCallingOptions() {
+ contextRunner.withPropertyValues("spring.ai.moonshot.chat.options.model=abab6-chat").run(context -> {
+
+ MoonshotChatModel chatModel = context.getBean(MoonshotChatModel.class);
+
+ // Test weatherFunction
+ UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
+
+ PortableFunctionCallingOptions functionOptions = FunctionCallingOptions.builder()
+ .withFunction("weatherFunction")
+ .build();
+
+ ChatResponse response = chatModel.call(new Prompt(List.of(userMessage), functionOptions));
+
+ logger.info("Response: {}", response);
+ });
+ }
+
+ @Test
+ void streamFunctionCallTest() {
+ contextRunner.withPropertyValues("spring.ai.moonshot.chat.options.model=abab6-chat").run(context -> {
+
+ MoonshotChatModel chatModel = context.getBean(MoonshotChatModel.class);
+
+ // Test weatherFunction
+ UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
+
+ Flux response = chatModel.stream(new Prompt(List.of(userMessage),
+ MoonshotChatOptions.builder().withFunction("weatherFunction").build()));
+
+ String content = response.collectList()
+ .block()
+ .stream()
+ .map(ChatResponse::getResults)
+ .flatMap(List::stream)
+ .map(Generation::getOutput)
+ .map(AssistantMessage::getContent)
+ .collect(Collectors.joining());
+ logger.info("Response: {}", content);
+
+ assertThat(content).containsAnyOf("30.0", "30");
+ assertThat(content).containsAnyOf("10.0", "10");
+ assertThat(content).containsAnyOf("15.0", "15");
+
+ // Test weatherFunctionTwo
+ response = chatModel.stream(new Prompt(List.of(userMessage),
+ MoonshotChatOptions.builder().withFunction("weatherFunctionTwo").build()));
+
+ content = response.collectList()
+ .block()
+ .stream()
+ .map(ChatResponse::getResults)
+ .flatMap(List::stream)
+ .map(Generation::getOutput)
+ .map(AssistantMessage::getContent)
+ .collect(Collectors.joining());
+ logger.info("Response: {}", content);
+
+ assertThat(content).containsAnyOf("30.0", "30");
+ assertThat(content).containsAnyOf("10.0", "10");
+ assertThat(content).containsAnyOf("15.0", "15");
+ });
+ }
+
+ @Configuration
+ static class Config {
+
+ @Bean
+ @Description("Get the weather in location")
+ public Function weatherFunction() {
+ return new MockWeatherService();
+ }
+
+ // Relies on the Request's JsonClassDescription annotation to provide the
+ // function description.
+ @Bean
+ public Function weatherFunctionTwo() {
+ MockWeatherService weatherService = new MockWeatherService();
+ return (weatherService::apply);
+ }
+
+ }
+
+}
\ No newline at end of file
diff --git a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackWrapperIT.java b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackWrapperIT.java
new file mode 100644
index 00000000000..0e9113d0d08
--- /dev/null
+++ b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/FunctionCallbackWrapperIT.java
@@ -0,0 +1,120 @@
+/*
+ * 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.autoconfigure.moonshot.tool;
+
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.ai.autoconfigure.moonshot.MoonshotAutoConfiguration;
+import org.springframework.ai.autoconfigure.retry.SpringAiRetryAutoConfiguration;
+import org.springframework.ai.chat.messages.AssistantMessage;
+import org.springframework.ai.chat.messages.UserMessage;
+import org.springframework.ai.chat.model.ChatResponse;
+import org.springframework.ai.chat.model.Generation;
+import org.springframework.ai.chat.prompt.Prompt;
+import org.springframework.ai.model.function.FunctionCallback;
+import org.springframework.ai.model.function.FunctionCallbackWrapper;
+import org.springframework.ai.moonshot.MoonshotChatModel;
+import org.springframework.ai.moonshot.MoonshotChatOptions;
+import org.springframework.boot.autoconfigure.AutoConfigurations;
+import org.springframework.boot.autoconfigure.web.client.RestClientAutoConfiguration;
+import org.springframework.boot.test.context.runner.ApplicationContextRunner;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import reactor.core.publisher.Flux;
+
+import java.util.List;
+import java.util.stream.Collectors;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * @author Geng Rong
+ */
+@EnabledIfEnvironmentVariable(named = "MOONSHOT_API_KEY", matches = ".*")
+public class FunctionCallbackWrapperIT {
+
+ private final Logger logger = LoggerFactory.getLogger(FunctionCallbackWrapperIT.class);
+
+ private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
+ .withPropertyValues("spring.ai.moonshot.apiKey=" + System.getenv("MOONSHOT_API_KEY"))
+ .withConfiguration(AutoConfigurations.of(SpringAiRetryAutoConfiguration.class,
+ RestClientAutoConfiguration.class, MoonshotAutoConfiguration.class))
+ .withUserConfiguration(Config.class);
+
+ @Test
+ void functionCallTest() {
+ contextRunner.withPropertyValues("spring.ai.moonshot.chat.options.model=abab6-chat").run(context -> {
+
+ MoonshotChatModel chatModel = context.getBean(MoonshotChatModel.class);
+
+ UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
+
+ ChatResponse response = chatModel.call(new Prompt(List.of(userMessage),
+ MoonshotChatOptions.builder().withFunction("WeatherInfo").build()));
+
+ logger.info("Response: {}", response);
+
+ assertThat(response.getResult().getOutput().getContent()).contains("30", "10", "15");
+
+ });
+ }
+
+ @Test
+ void streamFunctionCallTest() {
+ contextRunner.withPropertyValues("spring.ai.moonshot.chat.options.model=abab6-chat").run(context -> {
+
+ MoonshotChatModel chatModel = context.getBean(MoonshotChatModel.class);
+
+ UserMessage userMessage = new UserMessage("What's the weather like in San Francisco, Tokyo, and Paris?");
+
+ Flux response = chatModel.stream(new Prompt(List.of(userMessage),
+ MoonshotChatOptions.builder().withFunction("WeatherInfo").build()));
+
+ String content = response.collectList()
+ .block()
+ .stream()
+ .map(ChatResponse::getResults)
+ .flatMap(List::stream)
+ .map(Generation::getOutput)
+ .map(AssistantMessage::getContent)
+ .collect(Collectors.joining());
+ logger.info("Response: {}", content);
+
+ assertThat(content).containsAnyOf("30.0", "30");
+ assertThat(content).containsAnyOf("10.0", "10");
+ assertThat(content).containsAnyOf("15.0", "15");
+
+ });
+ }
+
+ @Configuration
+ static class Config {
+
+ @Bean
+ public FunctionCallback weatherFunctionInfo() {
+
+ return FunctionCallbackWrapper.builder(new MockWeatherService())
+ .withName("WeatherInfo")
+ .withDescription("Get the weather in location")
+ .withResponseConverter((response) -> "" + response.temp() + response.unit())
+ .build();
+ }
+
+ }
+
+}
\ No newline at end of file
diff --git a/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/MockWeatherService.java b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/MockWeatherService.java
new file mode 100644
index 00000000000..204105d7e32
--- /dev/null
+++ b/spring-ai-spring-boot-autoconfigure/src/test/java/org/springframework/ai/autoconfigure/moonshot/tool/MockWeatherService.java
@@ -0,0 +1,94 @@
+/*
+ * 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.autoconfigure.moonshot.tool;
+
+import com.fasterxml.jackson.annotation.JsonClassDescription;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonInclude.Include;
+import com.fasterxml.jackson.annotation.JsonProperty;
+import com.fasterxml.jackson.annotation.JsonPropertyDescription;
+
+import java.util.function.Function;
+
+/**
+ * Mock 3rd party weather service.
+ *
+ * @author Geng Rong
+ */
+public class MockWeatherService implements Function {
+
+ /**
+ * Weather Function request.
+ */
+ @JsonInclude(Include.NON_NULL)
+ @JsonClassDescription("Weather API request")
+ public record Request(@JsonProperty(required = true,
+ value = "location") @JsonPropertyDescription("The city and state e.g. San Francisco, CA") String location,
+ @JsonProperty(required = true, value = "lat") @JsonPropertyDescription("The city latitude") double lat,
+ @JsonProperty(required = true, value = "lon") @JsonPropertyDescription("The city longitude") double lon,
+ @JsonProperty(required = true, value = "unit") @JsonPropertyDescription("Temperature unit") Unit unit) {
+ }
+
+ /**
+ * Temperature units.
+ */
+ public enum Unit {
+
+ /**
+ * Celsius.
+ */
+ C("metric"),
+ /**
+ * Fahrenheit.
+ */
+ F("imperial");
+
+ /**
+ * Human readable unit name.
+ */
+ public final String unitName;
+
+ private Unit(String text) {
+ this.unitName = text;
+ }
+
+ }
+
+ /**
+ * Weather Function response.
+ */
+ public record Response(double temp, double feels_like, double temp_min, double temp_max, int pressure, int humidity,
+ Unit unit) {
+ }
+
+ @Override
+ public Response apply(Request request) {
+
+ double temperature = 0;
+ if (request.location().contains("Paris")) {
+ temperature = 15;
+ }
+ else if (request.location().contains("Tokyo")) {
+ temperature = 10;
+ }
+ else if (request.location().contains("San Francisco")) {
+ temperature = 30;
+ }
+
+ return new Response(temperature, 15, 20, 2, 53, 45, Unit.C);
+ }
+
+}
\ No newline at end of file
diff --git a/spring-ai-spring-boot-starters/spring-ai-starter-moonshot/pom.xml b/spring-ai-spring-boot-starters/spring-ai-starter-moonshot/pom.xml
new file mode 100644
index 00000000000..2c2b19a84f7
--- /dev/null
+++ b/spring-ai-spring-boot-starters/spring-ai-starter-moonshot/pom.xml
@@ -0,0 +1,42 @@
+
+
+ 4.0.0
+
+ org.springframework.ai
+ spring-ai
+ 1.0.0-SNAPSHOT
+ ../../pom.xml
+
+ spring-ai-moonshot-spring-boot-starter
+ jar
+ Spring AI Starter - Moonshot
+ Spring AI Moonshot Auto Configuration
+ https://github.com/spring-projects/spring-ai
+
+
+ https://github.com/spring-projects/spring-ai
+ git://github.com/spring-projects/spring-ai.git
+ git@github.com:spring-projects/spring-ai.git
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+
+ org.springframework.ai
+ spring-ai-spring-boot-autoconfigure
+ ${project.parent.version}
+
+
+
+ org.springframework.ai
+ spring-ai-moonshot
+ ${project.parent.version}
+
+
+
+