Remove PaLM Model support

This commit is contained in:
Mark Pollack
2024-06-22 11:53:48 -04:00
parent 810a18ee04
commit 64d0b13896
26 changed files with 0 additions and 2280 deletions

View File

@@ -1,4 +0,0 @@
[VertexAI PaLM2 Chat Documentation](https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/api/chat/vertexai-palm2-chat.html)
[VertexAI PaLM2 Embedding Documentation](https://docs.spring.io/spring-ai/reference/1.0-SNAPSHOT/api/embeddings/vertexai-embeddings.html)

View File

@@ -1,60 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>spring-ai-vertex-ai-palm2</artifactId>
<packaging>jar</packaging>
<name>Spring AI Model - Vertex AI PaLM2</name>
<description>Vertex AI PaLM2 models support</description>
<url>https://github.com/spring-projects/spring-ai</url>
<scm>
<url>https://github.com/spring-projects/spring-ai</url>
<connection>git://github.com/spring-projects/spring-ai.git</connection>
<developerConnection>git@github.com:spring-projects/spring-ai.git</developerConnection>
</scm>
<dependencies>
<!-- production dependencies -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-core</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Spring Framework -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</dependency>
<!-- test dependencies -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-test</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

View File

@@ -1,113 +0,0 @@
/*
* 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.vertexai.palm2;
import java.util.List;
import java.util.stream.Collectors;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.chat.prompt.ChatOptions;
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.ModelOptionsUtils;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.GenerateMessageRequest;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.GenerateMessageResponse;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.MessagePrompt;
import org.springframework.ai.chat.messages.MessageType;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
/**
* @author Christian Tzolov
*/
public class VertexAiPaLm2ChatModel implements ChatModel {
private final VertexAiPaLm2Api vertexAiApi;
private final VertexAiPaLm2ChatOptions defaultOptions;
public VertexAiPaLm2ChatModel(VertexAiPaLm2Api vertexAiApi) {
this(vertexAiApi,
VertexAiPaLm2ChatOptions.builder().withTemperature(0.7f).withCandidateCount(1).withTopK(20).build());
}
public VertexAiPaLm2ChatModel(VertexAiPaLm2Api vertexAiApi, VertexAiPaLm2ChatOptions defaultOptions) {
Assert.notNull(defaultOptions, "Default options must not be null!");
Assert.notNull(vertexAiApi, "VertexAiPaLm2Api must not be null!");
this.vertexAiApi = vertexAiApi;
this.defaultOptions = defaultOptions;
}
@Override
public ChatResponse call(Prompt prompt) {
GenerateMessageRequest request = createRequest(prompt);
GenerateMessageResponse response = this.vertexAiApi.generateMessage(request);
List<Generation> generations = response.candidates()
.stream()
.map(vmsg -> new Generation(vmsg.content()))
.toList();
return new ChatResponse(generations);
}
/**
* Accessible for testing.
*/
GenerateMessageRequest createRequest(Prompt prompt) {
String vertexContext = prompt.getInstructions()
.stream()
.filter(m -> m.getMessageType() == MessageType.SYSTEM)
.map(m -> m.getContent())
.collect(Collectors.joining(System.lineSeparator()));
List<VertexAiPaLm2Api.Message> vertexMessages = prompt.getInstructions()
.stream()
.filter(m -> m.getMessageType() == MessageType.USER || m.getMessageType() == MessageType.ASSISTANT)
.map(m -> new VertexAiPaLm2Api.Message(m.getMessageType().getValue(), m.getContent()))
.toList();
Assert.isTrue(!CollectionUtils.isEmpty(vertexMessages), "No user or assistant messages found in the prompt!");
var vertexPrompt = new MessagePrompt(vertexContext, vertexMessages);
GenerateMessageRequest request = new GenerateMessageRequest(vertexPrompt);
if (this.defaultOptions != null) {
request = ModelOptionsUtils.merge(request, this.defaultOptions, GenerateMessageRequest.class);
}
if (prompt.getOptions() != null) {
VertexAiPaLm2ChatOptions updatedRuntimeOptions = ModelOptionsUtils.copyToTarget(prompt.getOptions(),
ChatOptions.class, VertexAiPaLm2ChatOptions.class);
request = ModelOptionsUtils.merge(updatedRuntimeOptions, request, GenerateMessageRequest.class);
}
return request;
}
@Override
public ChatOptions getDefaultOptions() {
return VertexAiPaLm2ChatOptions.fromOptions(this.defaultOptions);
}
}

View File

@@ -1,139 +0,0 @@
/*
* 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.vertexai.palm2;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.ai.chat.prompt.ChatOptions;
/**
* @author Christian Tzolov
*/
@JsonInclude(Include.NON_NULL)
public class VertexAiPaLm2ChatOptions implements ChatOptions {
// @formatter:off
/**
* Controls the randomness of the output. Values can range over [0.0,1.0], inclusive.
* A value closer to 1.0 will produce responses that are more varied, while a value
* closer to 0.0 will typically result in less surprising responses from the
* generative. This value specifies default to be used by the backend while making the
* call to the generative.
*/
private @JsonProperty("temperature") Float temperature;
/**
* The number of generated response messages to return. This value must be between [1,
* 8], inclusive. Defaults to 1.
*/
private @JsonProperty("candidateCount") Integer candidateCount;
/**
* The maximum cumulative probability of tokens to consider when sampling. The
* generative uses combined Top-k and nucleus sampling. Nucleus sampling considers the
* smallest set of tokens whose probability sum is at least topP.
*/
private @JsonProperty("topP") Float topP;
/**
* The maximum number of tokens to consider when sampling. The generative uses
* combined Top-k and nucleus sampling. Top-k sampling considers the set of topK most
* probable tokens.
*/
private @JsonProperty("topK") Integer topK;
// @formatter:on
public static Builder builder() {
return new Builder();
}
public static class Builder {
private VertexAiPaLm2ChatOptions options = new VertexAiPaLm2ChatOptions();
public Builder withTemperature(Float temperature) {
this.options.temperature = temperature;
return this;
}
public Builder withCandidateCount(Integer candidateCount) {
this.options.candidateCount = candidateCount;
return this;
}
public Builder withTopP(Float topP) {
this.options.topP = topP;
return this;
}
public Builder withTopK(Integer topK) {
this.options.topK = topK;
return this;
}
public VertexAiPaLm2ChatOptions build() {
return this.options;
}
}
@Override
public Float getTemperature() {
return this.temperature;
}
public void setTemperature(Float temperature) {
this.temperature = temperature;
}
public Integer getCandidateCount() {
return this.candidateCount;
}
public void setCandidateCount(Integer candidateCount) {
this.candidateCount = candidateCount;
}
@Override
public Float getTopP() {
return this.topP;
}
public void setTopP(Float topP) {
this.topP = topP;
}
@Override
public Integer getTopK() {
return this.topK;
}
public void setTopK(Integer topK) {
this.topK = topK;
}
public static VertexAiPaLm2ChatOptions fromOptions(VertexAiPaLm2ChatOptions fromOptions) {
return VertexAiPaLm2ChatOptions.builder()
.withTemperature(fromOptions.getTemperature())
.withCandidateCount(fromOptions.getCandidateCount())
.withTopP(fromOptions.getTopP())
.withTopK(fromOptions.getTopK())
.build();
}
}

View File

@@ -1,55 +0,0 @@
/*
* 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.vertexai.palm2;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.springframework.ai.document.Document;
import org.springframework.ai.embedding.AbstractEmbeddingModel;
import org.springframework.ai.embedding.Embedding;
import org.springframework.ai.embedding.EmbeddingRequest;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
/**
* @author Christian Tzolov
*/
public class VertexAiPaLm2EmbeddingModel extends AbstractEmbeddingModel {
private final VertexAiPaLm2Api vertexAiApi;
public VertexAiPaLm2EmbeddingModel(VertexAiPaLm2Api vertexAiApi) {
this.vertexAiApi = vertexAiApi;
}
@Override
public List<Double> embed(Document document) {
return embed(document.getContent());
}
@Override
public EmbeddingResponse call(EmbeddingRequest request) {
List<VertexAiPaLm2Api.Embedding> vertexEmbeddings = this.vertexAiApi.batchEmbedText(request.getInstructions());
AtomicInteger indexCounter = new AtomicInteger(0);
List<Embedding> embeddings = vertexEmbeddings.stream()
.map(vm -> new Embedding(vm.value(), indexCounter.getAndIncrement()))
.toList();
return new EmbeddingResponse(embeddings);
}
}

View File

@@ -1,42 +0,0 @@
/*
* 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.vertexai.palm2.aot;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import static org.springframework.ai.aot.AiRuntimeHints.findJsonAnnotatedClassesInPackage;
/**
* The VertexRuntimeHints class is responsible for registering runtime hints for Vertex AI
* API classes.
*
* @author Josh Long
* @author Christian Tzolov
* @author Mark Pollack
*/
public class VertexRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
var mcs = MemberCategory.values();
for (var tr : findJsonAnnotatedClassesInPackage(VertexAiPaLm2Api.class))
hints.reflection().registerType(tr, mcs);
}
}

View File

@@ -1,599 +0,0 @@
/*
* 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.vertexai.palm2.api;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.Assert;
import org.springframework.web.client.ResponseErrorHandler;
import org.springframework.web.client.RestClient;
// @formatter:off
/**
* Vertex AI API client for the Generative Language model.
* https://developers.generativeai.google/api/rest/generativelanguage
* https://cloud.google.com/vertex-ai/docs/generative-ai/learn/streaming
*
* Provides methods to generate a response from the model given an input
* https://developers.generativeai.google/api/rest/generativelanguage/models/generateMessage
*
* as well as to generate embeddings for the input text:
* https://developers.generativeai.google/api/rest/generativelanguage/models/embedText
*
*
* Supported models:
*
* <pre>
* name=models/chat-bison-001,
* version=001,
* displayName=Chat Bison,
* description=Chat-optimized generative language model.,
* inputTokenLimit=4096,
* outputTokenLimit=1024,
* supportedGenerationMethods=[generateMessage, countMessageTokens],
* temperature=0.25,
* topP=0.95,
* topK=40
*
* name=models/text-bison-001,
* version=001,
* displayName=Text Bison,
* description=Model targeted for text generation.,
* inputTokenLimit=8196,
* outputTokenLimit=1024,
* supportedGenerationMethods=[generateText, countTextTokens, createTunedTextModel],
* temperature=0.7,
* topP=0.95,
* topK=40
*
* name=models/embedding-gecko-001,
* version=001,
* displayName=Embedding Gecko, description=Obtain a distributed representation of a text.,
* inputTokenLimit=1024,
* outputTokenLimit=1,
* supportedGenerationMethods=[embedText, countTextTokens],
* temperature=null,
* topP=null,
* topK=null
* </pre>
*
* https://cloud.google.com/vertex-ai/generative-ai/docs/learn/models
*
* https://ai.google.dev/api/rest#rest-resource:-v1.models
*
* @author Christian Tzolov
*/
public class VertexAiPaLm2Api {
/**
* The default generation model. This model is used to generate responses for the
* input text.
*/
public static final String DEFAULT_GENERATE_MODEL = "chat-bison-001";
/**
* The default embedding model. This model is used to generate embeddings for the
* input text.
*/
public static final String DEFAULT_EMBEDDING_MODEL = "embedding-gecko-001";
/**
* The default base URL for accessing the Vertex AI API.
*/
public static final String DEFAULT_BASE_URL = "https://generativelanguage.googleapis.com/v1beta3";
private final RestClient restClient;
private final String apiKey;
private final String chatModel;
private final String embeddingModel;
/**
* Create a new chat completion api.
* @param apiKey vertex apiKey.
*/
public VertexAiPaLm2Api(String apiKey) {
this(DEFAULT_BASE_URL, apiKey, DEFAULT_GENERATE_MODEL, DEFAULT_EMBEDDING_MODEL, RestClient.builder());
}
/**
* Create a new chat completion api.
* @param baseUrl api base URL.
* @param apiKey vertex apiKey.
* @param model vertex model.
* @param embeddingModel vertex embedding model.
* @param restClientBuilder RestClient builder.
*/
public VertexAiPaLm2Api(String baseUrl, String apiKey, String model, String embeddingModel,
RestClient.Builder restClientBuilder) {
this.chatModel = model;
this.embeddingModel = embeddingModel;
this.apiKey = apiKey;
Consumer<HttpHeaders> jsonContentHeaders = headers -> {
headers.setAccept(List.of(MediaType.APPLICATION_JSON));
headers.setContentType(MediaType.APPLICATION_JSON);
};
ResponseErrorHandler responseErrorHandler = new ResponseErrorHandler() {
@Override
public boolean hasError(ClientHttpResponse response) throws IOException {
return response.getStatusCode().isError();
}
@Override
public void handleError(ClientHttpResponse response) throws IOException {
if (response.getStatusCode().isError()) {
throw new RuntimeException(String.format("%s - %s", response.getStatusCode().value(),
new ObjectMapper().readValue(response.getBody(), ResponseError.class)));
}
}
};
this.restClient = restClientBuilder.baseUrl(baseUrl)
.defaultHeaders(jsonContentHeaders)
.defaultStatusHandler(responseErrorHandler)
.build();
}
/**
* Generates a response from the model given an input.
* @param request Request body.
* @return Response body.
*/
@SuppressWarnings("null")
public GenerateMessageResponse generateMessage(GenerateMessageRequest request) {
Assert.notNull(request, "The request body can not be null.");
return this.restClient.post()
.uri("/models/{model}:generateMessage?key={apiKey}", this.chatModel, this.apiKey)
.body(request)
.retrieve()
.body(GenerateMessageResponse.class);
}
/**
* Generates a response from the model given an input.
* @param text Text to embed.
* @return Embedding response.
*/
public Embedding embedText(String text) {
Assert.hasText(text, "The text can not be null or empty.");
@JsonInclude(Include.NON_NULL)
record EmbeddingResponse(Embedding embedding) {
}
EmbeddingResponse response = this.restClient.post()
.uri("/models/{model}:embedText?key={apiKey}", this.embeddingModel, this.apiKey)
.body(Map.of("text", text))
.retrieve()
.body(EmbeddingResponse.class);
return response != null ? response.embedding() : null;
}
@JsonInclude(Include.NON_NULL)
record BatchEmbeddingResponse(List<Embedding> embeddings) {
}
/**
* Generates a response from the model given an input.
* @param texts List of texts to embed.
* @return Embedding response containing a list of embeddings.
*/
public List<Embedding> batchEmbedText(List<String> texts) {
Assert.notNull(texts, "The texts can not be null.");
BatchEmbeddingResponse response = this.restClient.post()
.uri("/models/{model}:batchEmbedText?key={apiKey}", this.embeddingModel, this.apiKey)
// https://developers.generativeai.google/api/rest/generativelanguage/models/batchEmbedText#request-body
.body(Map.of("texts", texts))
.retrieve()
.body(BatchEmbeddingResponse.class);
return response != null ? response.embeddings() : null;
}
/**
* Returns the number of tokens in the message prompt.
* @param prompt Message prompt to count tokens for.
* @return Number of tokens in the message prompt.
*/
public Integer countMessageTokens(MessagePrompt prompt) {
Assert.notNull(prompt, "The message prompt can not be null.");
record TokenCount(@JsonProperty("tokenCount") Integer tokenCount) {
}
TokenCount tokenCountResponse = this.restClient.post()
.uri("/models/{model}:countMessageTokens?key={apiKey}", this.chatModel, this.apiKey)
.body(Map.of("prompt", prompt))
.retrieve()
.body(TokenCount.class);
return tokenCountResponse != null ? tokenCountResponse.tokenCount() : null;
}
/**
* Returns the list of models available for use.
* @return List of models available for use.
*/
public List<String> listModels() {
@JsonInclude(Include.NON_NULL)
record ModelList(@JsonProperty("models") List<ModelName> models) {
record ModelName(String name) {
}
}
ModelList modelList = this.restClient.get()
.uri("/models?key={apiKey}", this.apiKey)
.retrieve()
.body(ModelList.class);
return modelList == null ? List.of() :
modelList.models().stream()
.map(ModelList.ModelName::name)
.toList();
}
/**
* Returns the model details.
* @param modelName Name of the model to get details for.
* @return Model details.
*/
public Model getModel(String modelName) {
Assert.hasText(modelName, "The model name can not be null or empty.");
if (modelName.startsWith("models/")) {
modelName = modelName.substring("models/".length());
}
return this.restClient.get()
.uri("/models/{model}?key={apiKey}", modelName, this.apiKey)
.retrieve()
.body(Model.class);
}
/**
* API error response.
*
* @param error Error details.
*/
@JsonInclude(Include.NON_NULL)
public record ResponseError(
@JsonProperty("error") Error error) {
/**
* Error details.
*
* @param message Error message.
* @param code Error code.
* @param status Error status.
*/
@JsonInclude(Include.NON_NULL)
public record Error(
@JsonProperty("message") String message,
@JsonProperty("code") String code,
@JsonProperty("status") String status) {
}
}
/**
* Information about a Generative Language Model.
*
* @param name The resource name of the Model. Format: `models/{model} with a {model}
* naming convention of:`
*
* <pre>
* {baseModelId}-{version}
* </pre>
* @param baseModelId The name of the base model, pass this to the generation request.
* @param version The version of the model. This represents the major version.
* @param displayName The human-readable name of the model. E.g. "Chat Bison". The
* name can be up to 128 characters long and can consist of any UTF-8 characters.
* @param description A short description of the model.
* @param inputTokenLimit Maximum number of input tokens allowed for this model.
* @param outputTokenLimit Maximum number of output tokens allowed for this model.
* @param supportedGenerationMethods List of supported generation methods for this
* model. The method names are defined as Pascal case strings, such as generateMessage
* which correspond to API methods.
* @param temperature Controls the randomness of the output. Values can range over
* [0.0,1.0], inclusive. A value closer to 1.0 will produce responses that are more
* varied, while a value closer to 0.0 will typically result in less surprising
* responses from the model. This value specifies default to be used by the backend
* while making the call to the model.
* @param topP For Nucleus sampling. Nucleus sampling considers the smallest set of
* tokens whose probability sum is at least topP. This value specifies default to be
* used by the backend while making the call to the model.
* @param topK For Top-k sampling. Top-k sampling considers the set of topK most
* probable tokens. This value specifies default to be used by the backend while
* making the call to the model.
*/
@JsonInclude(Include.NON_NULL)
public record Model(
@JsonProperty("name") String name,
@JsonProperty("baseModelId") String baseModelId,
@JsonProperty("version") String version,
@JsonProperty("displayName") String displayName,
@JsonProperty("description") String description,
@JsonProperty("inputTokenLimit") Integer inputTokenLimit,
@JsonProperty("outputTokenLimit") Integer outputTokenLimit,
@JsonProperty("supportedGenerationMethods") List<String> supportedGenerationMethods,
@JsonProperty("temperature") Float temperature,
@JsonProperty("topP") Float topP,
@JsonProperty("topK") Integer topK) {
}
/**
* A list of floats representing the embedding.
*
* @param value The embedding values.
*/
@JsonInclude(Include.NON_NULL)
public record Embedding(
@JsonProperty("value") List<Double> value) {
}
/**
* The base unit of structured text. A Message includes an author and the content of
* the Message. The author is used to tag messages when they are fed to the model as
* text.
*
* @param author (optional) Author of the message. This serves as a key for tagging
* the content of this Message when it is fed to the model as text.The author can be
* any alphanumeric string.
* @param content The text content of the structured Message.
* @param citationMetadata (output only) Citation information for model-generated
* content in this Message. If this Message was generated as output from the model,
* this field may be populated with attribution information for any text included in
* the content. This field is used only on output.
*/
@JsonInclude(Include.NON_NULL)
public record Message(
@JsonProperty("author") String author,
@JsonProperty("content") String content,
@JsonProperty("citationMetadata") CitationMetadata citationMetadata) {
/**
* Short-hand constructor for a message without citation metadata.
* @param author (optional) Author of the message.
* @param content The text content of the structured Message.
*/
public Message(String author, String content) {
this(author, content, null);
}
/**
* A collection of source attributions for a piece of content.
*
* Citations to sources for a specific response.
*/
@JsonInclude(Include.NON_NULL)
public record CitationMetadata(
@JsonProperty("citationSources") List<CitationSource> citationSources) {
}
/**
* A citation to a source for a portion of a specific response.
*
* @param startIndex (optional) Start of segment of the response that is
* attributed to this source. Index indicates the start of the segment, measured
* in bytes.
* @param endIndex (optional) End of the attributed segment, exclusive.
* @param uri (optional) URI that is attributed as a source for a portion of the
* text.
* @param license (optional) License for the GitHub project that is attributed as
* a source for segment.License info is required for code citations.
*/
@JsonInclude(Include.NON_NULL)
public record CitationSource(
@JsonProperty("startIndex") Integer startIndex,
@JsonProperty("endIndex") Integer endIndex,
@JsonProperty("uri") String uri,
@JsonProperty("license") String license) {
}
}
/**
* All of the structured input text passed to the model as a prompt.
*
* A MessagePrompt contains a structured set of fields that provide context for the
* conversation, examples of user input/model output message pairs that prime the
* model to respond in different ways, and the conversation history or list of
* messages representing the alternating turns of the conversation between the user
* and the model.
*
* @param context (optional) Text that should be provided to the model first to ground
* the response. If not empty, this context will be given to the model first before
* the examples and messages. When using a context be sure to provide it with every
* request to maintain continuity. This field can be a description of your prompt to
* the model to help provide context and guide the responses. Examples: "Translate the
* phrase from English to French." or "Given a statement, classify the sentiment as
* happy, sad or neutral." Anything included in this field will take precedence over
* message history if the total input size exceeds the model's inputTokenLimit and the
* input request is truncated.
* @param examples (optional) Examples of what the model should generate. This
* includes both user input and the response that the model should emulate. These
* examples are treated identically to conversation messages except that they take
* precedence over the history in messages: If the total input size exceeds the
* model's inputTokenLimit the input will be truncated. Items will be dropped from
* messages before examples.
* @param messages (optional) A snapshot of the recent conversation history sorted
* chronologically. Turns alternate between two authors. If the total input size
* exceeds the model's inputTokenLimit the input will be truncated: The oldest items
* will be dropped from messages.
*/
@JsonInclude(Include.NON_NULL)
public record MessagePrompt(
@JsonProperty("context") String context,
@JsonProperty("examples") List<Example> examples,
@JsonProperty("messages") List<Message> messages) {
/**
* Shortcut constructor for a message prompt without context.
* @param messages The conversation history used by the model.
*/
public MessagePrompt(List<Message> messages) {
this(null, null, messages);
}
/**
* Shortcut constructor for a message prompt without context.
* @param context An input/output example used to instruct the Model. It
* demonstrates how the model should respond or format its response.
* @param messages The conversation history used by the model.
*/
public MessagePrompt(String context, List<Message> messages) {
this(context, null, messages);
}
/**
* An input/output example used to instruct the Model. It demonstrates how the
* model should respond or format its response.
*
* @param input An example of an input Message from the user.
* @param output An example of an output Message from the model.
*/
@JsonInclude(Include.NON_NULL)
public record Example(
@JsonProperty("input") Message input,
@JsonProperty("output") Message output) {
}
}
/**
* Message generation request body.
*
* @param prompt The structured textual input given to the model as a prompt. Given a
* prompt, the model will return what it predicts is the next message in the
* discussion.
* @param temperature (optional) Controls the randomness of the output. Values can
* range over [0.0,1.0], inclusive. A value closer to 1.0 will produce responses that
* are more varied, while a value closer to 0.0 will typically result in less
* surprising responses from the model.
* @param candidateCount (optional) The number of generated response messages to
* return. This value must be between [1, 8], inclusive. If unset, this will default
* to 1.
* @param topP (optional) The maximum cumulative probability of tokens to consider
* when sampling. The model uses combined Top-k and nucleus sampling. Nucleus sampling
* considers the smallest set of tokens whose probability sum is at least topP.
* @param topK (optional) The maximum number of tokens to consider when sampling. The
* model uses combined Top-k and nucleus sampling. Top-k sampling considers the set of
* topK most probable tokens.
*/
@JsonInclude(Include.NON_NULL)
public record GenerateMessageRequest(
@JsonProperty("prompt") MessagePrompt prompt,
@JsonProperty("temperature") Float temperature,
@JsonProperty("candidateCount") Integer candidateCount,
@JsonProperty("topP") Float topP,
@JsonProperty("topK") Integer topK) {
/**
* Shortcut constructor to create a GenerateMessageRequest with only the prompt
* parameter.
* @param prompt The structured textual input given to the model as a prompt.
*/
public GenerateMessageRequest(MessagePrompt prompt) {
this(prompt, null, null, null, null);
}
/**
* Shortcut constructor to create a GenerateMessageRequest with only the prompt
* and temperature parameters.
* @param prompt The structured textual input given to the model as a prompt.
* @param temperature (optional) Controls the randomness of the output.
* @param topK (optional) The maximum number of tokens to consider when sampling.
*/
public GenerateMessageRequest(MessagePrompt prompt, Float temperature, Integer topK) {
this(prompt, temperature, null, null, topK);
}
}
/**
* The response from the model. This includes candidate messages and conversation
* history in the form of chronologically-ordered messages.
*
* @param candidates Candidate response messages from the model.
* @param messages The conversation history used by the model.
* @param filters A set of content filtering metadata for the prompt and response
* text. This indicates which SafetyCategory(s) blocked a candidate from this
* response, the lowest HarmProbability that triggered a block, and the HarmThreshold
* setting for that category.
*/
@JsonInclude(Include.NON_NULL)
public record GenerateMessageResponse(
@JsonProperty("candidates") List<Message> candidates,
@JsonProperty("messages") List<Message> messages,
@JsonProperty("filters") List<ContentFilter> filters) {
/**
* Content filtering metadata associated with processing a single request. It
* contains a reason and an optional supporting string. The reason may be
* unspecified.
*
* @param reason The reason content was blocked during request processing.
* @param message A string that describes the filtering behavior in more detail.
*/
@JsonInclude(Include.NON_NULL)
public record ContentFilter(
@JsonProperty("reason") BlockedReason reason,
@JsonProperty("message") String message) {
/**
* Reasons why content may have been blocked.
*/
public enum BlockedReason {
/**
* A blocked reason was not specified.
*/
BLOCKED_REASON_UNSPECIFIED,
/**
* Content was blocked by safety settings.
*/
SAFETY,
/**
* Content was blocked, but the reason is uncategorized.
*/
OTHER
}
}
}
}
// @formatter:on

View File

@@ -1,2 +0,0 @@
org.springframework.aot.hint.RuntimeHintsRegistrar=\
org.springframework.ai.vertexai.palm2.aot.VertexRuntimeHints

View File

@@ -1,145 +0,0 @@
/*
* 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.vertexai.palm2;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.model.Generation;
import org.springframework.ai.chat.messages.Message;
import org.springframework.ai.chat.messages.UserMessage;
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.converter.BeanOutputConverter;
import org.springframework.ai.converter.ListOutputConverter;
import org.springframework.ai.converter.MapOutputConverter;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.io.Resource;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@EnabledIfEnvironmentVariable(named = "PALM_API_KEY", matches = ".*")
class VertexAiPaLm2ChatGenerationClientIT {
@Autowired
private VertexAiPaLm2ChatModel chatModel;
@Value("classpath:/prompts/system-message.st")
private Resource systemResource;
@Test
void roleTest() {
String request = "Tell me about 3 famous pirates from the Golden Age of Piracy and why they did.";
String name = "Bob";
String voice = "pirate";
UserMessage userMessage = new UserMessage(request);
SystemPromptTemplate systemPromptTemplate = new SystemPromptTemplate(systemResource);
Message systemMessage = systemPromptTemplate.createMessage(Map.of("name", name, "voice", voice));
Prompt prompt = new Prompt(List.of(userMessage, systemMessage));
ChatResponse response = chatModel.call(prompt);
assertThat(response.getResult().getOutput().getContent()).contains("Bartholomew");
}
// @Test
void listOutputConverter() {
DefaultConversionService conversionService = new DefaultConversionService();
ListOutputConverter outputConverter = new ListOutputConverter(conversionService);
String format = outputConverter.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<String> list = outputConverter.convert(generation.getOutput().getContent());
assertThat(list).hasSize(5);
}
// @Test
void mapOutputConverter() {
MapOutputConverter outputConverter = new MapOutputConverter();
String format = outputConverter.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<String, Object> result = outputConverter.convert(generation.getOutput().getContent());
assertThat(result.get("numbers")).isEqualTo(Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9));
}
record ActorsFilmsRecord(String actor, List<String> movies) {
}
// @Test
void beanOutputConverterRecords() {
BeanOutputConverter<ActorsFilmsRecord> outputConverter = new BeanOutputConverter<>(ActorsFilmsRecord.class);
String format = outputConverter.getFormat();
String template = """
Generate the filmography of 5 movies for Tom Hanks.
{format}
""";
PromptTemplate promptTemplate = new PromptTemplate(template, Map.of("format", format));
Prompt prompt = new Prompt(promptTemplate.createMessage());
Generation generation = chatModel.call(prompt).getResult();
ActorsFilmsRecord actorsFilms = outputConverter.convert(generation.getOutput().getContent());
assertThat(actorsFilms.actor()).isEqualTo("Tom Hanks");
assertThat(actorsFilms.movies()).hasSize(5);
}
@SpringBootConfiguration
public static class TestConfiguration {
@Bean
public VertexAiPaLm2Api vertexAiApi() {
return new VertexAiPaLm2Api(System.getenv("PALM_API_KEY"));
}
@Bean
public VertexAiPaLm2ChatModel vertexAiEmbedding(VertexAiPaLm2Api vertexAiApi) {
return new VertexAiPaLm2ChatModel(vertexAiApi);
}
}
}

View File

@@ -1,88 +0,0 @@
/*
* 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.vertexai.palm2;
import org.junit.jupiter.api.Test;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.ai.chat.prompt.ChatOptionsBuilder;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Christian Tzolov
*/
public class VertexAiPaLm2ChatRequestTests {
VertexAiPaLm2ChatModel chatModel = new VertexAiPaLm2ChatModel(new VertexAiPaLm2Api("bla"));
@Test
public void createRequestWithDefaultOptions() {
var request = chatModel.createRequest(new Prompt("Test message content"));
assertThat(request.prompt().messages()).hasSize(1);
assertThat(request.candidateCount()).isEqualTo(1);
assertThat(request.temperature()).isEqualTo(0.7f);
assertThat(request.topK()).isEqualTo(20);
assertThat(request.topP()).isNull();
}
@Test
public void createRequestWithPromptVertexAiOptions() {
// Runtime options should override the default options.
VertexAiPaLm2ChatOptions promptOptions = VertexAiPaLm2ChatOptions.builder()
.withTemperature(0.8f)
.withTopP(0.5f)
.withTopK(99)
// .withCandidateCount(2)
.build();
var request = chatModel.createRequest(new Prompt("Test message content", promptOptions));
assertThat(request.prompt().messages()).hasSize(1);
assertThat(request.candidateCount()).isEqualTo(1);
assertThat(request.temperature()).isEqualTo(0.8f);
assertThat(request.topK()).isEqualTo(99);
assertThat(request.topP()).isEqualTo(0.5f);
}
@Test
public void createRequestWithPromptPortableChatOptions() {
// runtime options.
ChatOptions portablePromptOptions = ChatOptionsBuilder.builder()
.withTemperature(0.9f)
.withTopK(100)
.withTopP(0.6f)
.build();
var request = chatModel.createRequest(new Prompt("Test message content", portablePromptOptions));
assertThat(request.prompt().messages()).hasSize(1);
assertThat(request.candidateCount()).isEqualTo(1);
assertThat(request.temperature()).isEqualTo(0.9f);
assertThat(request.topK()).isEqualTo(100);
assertThat(request.topP()).isEqualTo(0.6f);
}
}

View File

@@ -1,77 +0,0 @@
/*
* 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.vertexai.palm2;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import static org.assertj.core.api.Assertions.assertThat;
@SpringBootTest
@EnabledIfEnvironmentVariable(named = "PALM_API_KEY", matches = ".*")
class VertexAiPaLm2EmbeddingModelIT {
@Autowired
private VertexAiPaLm2EmbeddingModel embeddingModel;
@Test
void simpleEmbedding() {
assertThat(embeddingModel).isNotNull();
EmbeddingResponse embeddingResponse = embeddingModel.embedForResponse(List.of("Hello World"));
assertThat(embeddingResponse.getResults()).hasSize(1);
assertThat(embeddingResponse.getResults().get(0).getOutput()).isNotEmpty();
assertThat(embeddingModel.dimensions()).isEqualTo(768);
}
@Test
void batchEmbedding() {
assertThat(embeddingModel).isNotNull();
EmbeddingResponse embeddingResponse = embeddingModel
.embedForResponse(List.of("Hello World", "World is big and salvation is near"));
assertThat(embeddingResponse.getResults()).hasSize(2);
assertThat(embeddingResponse.getResults().get(0).getOutput()).isNotEmpty();
assertThat(embeddingResponse.getResults().get(0).getIndex()).isEqualTo(0);
assertThat(embeddingResponse.getResults().get(1).getOutput()).isNotEmpty();
assertThat(embeddingResponse.getResults().get(1).getIndex()).isEqualTo(1);
assertThat(embeddingModel.dimensions()).isEqualTo(768);
}
@SpringBootConfiguration
public static class TestConfiguration {
@Bean
public VertexAiPaLm2Api vertexAiApi() {
return new VertexAiPaLm2Api(System.getenv("PALM_API_KEY"));
}
@Bean
public VertexAiPaLm2EmbeddingModel vertexAiEmbedding(VertexAiPaLm2Api vertexAiApi) {
return new VertexAiPaLm2EmbeddingModel(vertexAiApi);
}
}
}

View File

@@ -1,43 +0,0 @@
/*
* 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.vertexai.palm2.aot;
import org.junit.jupiter.api.Test;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
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;
class VertexRuntimeHintsTests {
@Test
void registerHints() {
RuntimeHints runtimeHints = new RuntimeHints();
VertexRuntimeHints vertexRuntimeHints = new VertexRuntimeHints();
vertexRuntimeHints.registerHints(runtimeHints, null);
Set<TypeReference> jsonAnnotatedClasses = findJsonAnnotatedClassesInPackage(VertexAiPaLm2Api.class);
for (TypeReference jsonAnnotatedClass : jsonAnnotatedClasses) {
assertThat(runtimeHints).matches(reflection().onType(jsonAnnotatedClass));
}
}
}

View File

@@ -1,124 +0,0 @@
/*
* 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.vertexai.palm2.api;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.Embedding;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.GenerateMessageRequest;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.GenerateMessageResponse;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.MessagePrompt;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Integration tests for {@link VertexAiPaLm2Api}. Requires a valid API key to be set via
* the {@code PALM_API_KEY} environment and at the moment Google enables is it only in the
* US region (so use VPN for testing).
*
* @author Christian Tzolov
*/
@EnabledIfEnvironmentVariable(named = "PALM_API_KEY", matches = ".*")
public class VertexAiPaLm2ApiIT {
VertexAiPaLm2Api vertexAiPaLm2Api = new VertexAiPaLm2Api(System.getenv("PALM_API_KEY"));
@Test
public void generateMessage() {
var prompt = new MessagePrompt(List.of(new VertexAiPaLm2Api.Message("0", "Hello, how are you?")));
GenerateMessageRequest request = new GenerateMessageRequest(prompt);
GenerateMessageResponse response = vertexAiPaLm2Api.generateMessage(request);
assertThat(response).isNotNull();
// Vertex returns the prompt messages in the response's messages list.
assertThat(response.messages()).hasSize(1);
assertThat(response.messages().get(0)).isEqualTo(prompt.messages().get(0));
// Vertex returns the answer in the response's candidates list.
assertThat(response.candidates()).hasSize(1);
assertThat(response.candidates().get(0).author()).isNotBlank();
assertThat(response.candidates().get(0).content()).isNotBlank();
}
@Test
public void embedText() {
var text = "Hello, how are you?";
Embedding response = vertexAiPaLm2Api.embedText(text);
assertThat(response).isNotNull();
assertThat(response.value()).hasSize(768);
}
@Test
public void batchEmbedText() {
var text = List.of("Hello, how are you?", "I am fine, thank you!");
List<Embedding> response = vertexAiPaLm2Api.batchEmbedText(text);
assertThat(response).isNotNull();
assertThat(response).hasSize(2);
assertThat(response.get(0).value()).hasSize(768);
assertThat(response.get(1).value()).hasSize(768);
}
@Test
public void countMessageTokens() {
var text = "Hello, how are you?";
var prompt = new MessagePrompt(List.of(new VertexAiPaLm2Api.Message("0", text)));
int response = vertexAiPaLm2Api.countMessageTokens(prompt);
assertThat(response).isEqualTo(17);
}
@Test
public void listModels() {
List<String> response = vertexAiPaLm2Api.listModels();
assertThat(response).isNotNull();
assertThat(response).hasSizeGreaterThan(0);
assertThat(response).contains("models/chat-bison-001", "models/text-bison-001", "models/embedding-gecko-001");
System.out.println(" - " + response.stream()
.map(vertexAiPaLm2Api::getModel)
.map(VertexAiPaLm2Api.Model::toString)
.collect(Collectors.joining("\n - ")));
}
@Test
public void getModel() {
VertexAiPaLm2Api.Model model = vertexAiPaLm2Api.getModel("models/chat-bison-001");
System.out.println(model);
assertThat(model).isNotNull();
assertThat(model.displayName()).isEqualTo("PaLM 2 Chat (Legacy)");
}
}

View File

@@ -1,149 +0,0 @@
/*
* 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.vertexai.palm2.api;
import java.util.List;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.Embedding;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.GenerateMessageRequest;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.GenerateMessageResponse;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.MessagePrompt;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api.GenerateMessageResponse.ContentFilter.BlockedReason;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.autoconfigure.web.client.RestClientTest;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.test.web.client.MockRestServiceServer;
import org.springframework.web.client.RestClient;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.content;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestToUriTemplate;
import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
/**
* @author Christian Tzolov
*/
@RestClientTest(VertexAiPaLm2ApiTests.Config.class)
public class VertexAiPaLm2ApiTests {
private final static String TEST_API_KEY = "test-api-key";
@Autowired
private VertexAiPaLm2Api client;
@Autowired
private MockRestServiceServer server;
@Autowired
private ObjectMapper objectMapper;
@AfterEach
void resetMockServer() {
server.reset();
}
@Test
public void generateMessage() throws JsonProcessingException {
GenerateMessageRequest request = new GenerateMessageRequest(
new MessagePrompt(List.of(new VertexAiPaLm2Api.Message("0", "Hello, how are you?"))));
GenerateMessageResponse expectedResponse = new GenerateMessageResponse(
List.of(new VertexAiPaLm2Api.Message("1", "Hello, how are you?")),
List.of(new VertexAiPaLm2Api.Message("0", "I'm fine, thank you.")),
List.of(new VertexAiPaLm2Api.GenerateMessageResponse.ContentFilter(BlockedReason.SAFETY, "reason")));
server
.expect(requestToUriTemplate("/models/{generative}:generateMessage?key={apiKey}",
VertexAiPaLm2Api.DEFAULT_GENERATE_MODEL, TEST_API_KEY))
.andExpect(method(HttpMethod.POST))
.andExpect(content().json(objectMapper.writeValueAsString(request)))
.andRespond(withSuccess(objectMapper.writeValueAsString(expectedResponse), MediaType.APPLICATION_JSON));
GenerateMessageResponse response = client.generateMessage(request);
assertThat(response).isEqualTo(expectedResponse);
server.verify();
}
@Test
public void embedText() throws JsonProcessingException {
String text = "Hello, how are you?";
Embedding expectedEmbedding = new Embedding(List.of(0.1, 0.2, 0.3));
server
.expect(requestToUriTemplate("/models/{generative}:embedText?key={apiKey}",
VertexAiPaLm2Api.DEFAULT_EMBEDDING_MODEL, TEST_API_KEY))
.andExpect(method(HttpMethod.POST))
.andExpect(content().json(objectMapper.writeValueAsString(Map.of("text", text))))
.andRespond(withSuccess(objectMapper.writeValueAsString(Map.of("embedding", expectedEmbedding)),
MediaType.APPLICATION_JSON));
Embedding embedding = client.embedText(text);
assertThat(embedding).isEqualTo(expectedEmbedding);
server.verify();
}
@Test
public void batchEmbedText() throws JsonProcessingException {
List<String> texts = List.of("Hello, how are you?", "I'm fine, thank you.");
List<Embedding> expectedEmbeddings = List.of(new Embedding(List.of(0.1, 0.2, 0.3)),
new Embedding(List.of(0.4, 0.5, 0.6)));
server
.expect(requestToUriTemplate("/models/{generative}:batchEmbedText?key={apiKey}",
VertexAiPaLm2Api.DEFAULT_EMBEDDING_MODEL, TEST_API_KEY))
.andExpect(method(HttpMethod.POST))
.andExpect(content().json(objectMapper.writeValueAsString(Map.of("texts", texts))))
.andRespond(withSuccess(objectMapper.writeValueAsString(Map.of("embeddings", expectedEmbeddings)),
MediaType.APPLICATION_JSON));
List<Embedding> embeddings = client.batchEmbedText(texts);
assertThat(embeddings).isEqualTo(expectedEmbeddings);
server.verify();
}
@SpringBootConfiguration
static class Config {
@Bean
public VertexAiPaLm2Api audioApi(RestClient.Builder builder) {
return new VertexAiPaLm2Api("", TEST_API_KEY, VertexAiPaLm2Api.DEFAULT_GENERATE_MODEL,
VertexAiPaLm2Api.DEFAULT_EMBEDDING_MODEL, builder);
}
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 233 KiB

View File

@@ -1,4 +0,0 @@
"You are a helpful AI assistant. Your name is {name}.
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}.

View File

@@ -72,7 +72,6 @@
<module>models/spring-ai-stability-ai</module>
<module>models/spring-ai-transformers</module>
<module>models/spring-ai-vertex-ai-gemini</module>
<module>models/spring-ai-vertex-ai-palm2</module>
<module>models/spring-ai-watsonx-ai</module>
<module>models/spring-ai-zhipuai</module>
<module>models/spring-ai-moonshot</module>
@@ -89,7 +88,6 @@
<module>spring-ai-spring-boot-starters/spring-ai-starter-stability-ai</module>
<module>spring-ai-spring-boot-starters/spring-ai-starter-transformers</module>
<module>spring-ai-spring-boot-starters/spring-ai-starter-vertex-ai-gemini</module>
<module>spring-ai-spring-boot-starters/spring-ai-starter-vertex-ai-palm2</module>
<module>spring-ai-spring-boot-starters/spring-ai-starter-watsonx-ai</module>
<module>spring-ai-spring-boot-starters/spring-ai-starter-zhipuai</module>
<module>spring-ai-spring-boot-starters/spring-ai-starter-moonshot</module>

View File

@@ -18,7 +18,6 @@
**** xref:api/chat/bedrock/bedrock-jurassic2.adoc[Jurassic2]
*** xref:api/chat/huggingface.adoc[Hugging Face]
*** xref:api/chat/google-vertexai.adoc[Google VertexAI]
**** xref:api/chat/vertexai-palm2-chat.adoc[VertexAI PaLM2 ]
**** xref:api/chat/vertexai-gemini-chat.adoc[VertexAI Gemini]
***** xref:api/chat/functions/vertexai-gemini-chat-functions.adoc[Function Calling]
*** xref:api/chat/mistralai-chat.adoc[Mistral AI]

View File

@@ -1,222 +0,0 @@
= VertexAI PaLM2 Chat
WARNING:
> https://ai.google.dev/palm_docs/deprecation[As of February 15, 2024, the PaLM2 API for use with Google AI services and tooling is deprecated].
In 6 months, the PaLM API will be decommissioned, meaning that users won't be able to use a PaLM model in a prompt, tune a new PaLM model, or run inference on PaLM-tuned models.
The link:https://developers.generativeai.google/api/rest/generativelanguage[Generative Language] PaLM API allows developers to build generative AI applications using the PaLM model. Large Language Models (LLMs) are a powerful, versatile type of machine learning model that enables computers to comprehend and generate natural language through a series of prompts. The PaLM API is based on Google's next generation LLM, PaLM. It excels at a variety of different tasks like code generation, reasoning, and writing. You can use the PaLM API to build generative AI applications for use cases like content generation, dialogue agents, summarization and classification systems, and more.
Based on the link:https://developers.generativeai.google/api/rest/generativelanguage/models[Models REST API].
== Prerequisites
To access the PaLM2 REST API you need to obtain an access API KEY form link:https://makersuite.google.com/app/apikey[makersuite].
NOTE: Currently the PaLM API it is not available outside US, but you can use VPN for testing.
The Spring AI project defines a configuration property named `spring.ai.vertex.ai.api-key` that you should set to the value of the `API Key` obtained.
Exporting an environment variable is one way to set that configuration property:
[source,shell]
----
export SPRING_AI_VERTEX_AI_API_KEY=<INSERT KEY HERE>
----
=== 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 VertexAI Chat Client.
To enable it add the following dependency to your project's Maven `pom.xml` file:
[source, xml]
----
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-vertex-ai-palm2-spring-boot-starter</artifactId>
</dependency>
----
or to your Gradle `build.gradle` build file.
[source,groovy]
----
dependencies {
implementation 'org.springframework.ai:spring-ai-vertex-ai-palm2-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
The prefix `spring.ai.vertex.ai` is used as the property prefix that lets you connect to VertexAI.
[cols="3,5,1"]
|====
| Property | Description | Default
| spring.ai.vertex.ai.ai.base-url | The URL to connect to | https://generativelanguage.googleapis.com/v1beta3
| spring.ai.vertex.ai.api-key | The API Key | -
|====
The prefix `spring.ai.vertex.ai.chat` is the property prefix that lets you configure the chat model implementation for VertexAI Chat.
[cols="3,5,1"]
|====
| Property | Description | Default
| spring.ai.vertex.ai.chat.enabled | Enable Vertex AI PaLM API chat model. | true
| spring.ai.vertex.ai.chat.model | This is the https://cloud.google.com/vertex-ai/docs/generative-ai/model-reference/text-chat[Vertex Chat model] to use | chat-bison-001
| spring.ai.vertex.ai.chat.options.temperature | Controls the randomness of the output. Values can range over [0.0,1.0], inclusive. A value closer to 1.0 will produce responses that are more varied, while a value closer to 0.0 will typically result in less surprising responses from the generative. This value specifies default to be used by the backend while making the call to the generative. | 0.7
| spring.ai.vertex.ai.chat.options.topK | The maximum number of tokens to consider when sampling. The generative uses combined Top-k and nucleus sampling. Top-k sampling considers the set of topK most probable tokens. | 20
| spring.ai.vertex.ai.chat.options.topP | The maximum cumulative probability of tokens to consider when sampling. The generative uses combined Top-k and nucleus sampling. Nucleus sampling considers the smallest set of tokens whose probability sum is at least topP. | -
| spring.ai.vertex.ai.chat.options.candidateCount | The number of generated response messages to return. This value must be between [1, 8], inclusive. Defaults to 1. | 1
|====
TIP: All properties prefixed with `spring.ai.vertex.ai.chat.options` can be overridden at runtime by adding a request specific <<chat-options>> to the `Prompt` call.
== Runtime Options [[chat-options]]
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-vertex-ai-palm2/src/main/java/org/springframework/ai/vertexai/palm2/VertexAiPaLm2ChatOptions.java[VertexAiPaLm2ChatOptions.java] provides model configurations, such as the temperature, the topK, etc.
On start-up, the default options can be configured with the `VertexAiPaLm2ChatModel(api, options)` constructor or the `spring.ai.vertex.ai.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 temperature for a specific request:
[source,java]
----
ChatResponse response = chatModel.call(
new Prompt(
"Generate the names of 5 famous pirates.",
VertexAiPaLm2ChatOptions.builder()
.withTemperature(0.4)
.build()
));
----
TIP: In addition to the model specific `VertexAiPaLm2ChatOptions` you can use a portable https://github.com/spring-projects/spring-ai/blob/main/spring-ai-core/src/main/java/org/springframework/ai/chat/prompt/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/prompt/ChatOptionsBuilder.java[ChatOptionsBuilder#builder()].
== Sample Controller
https://start.spring.io/[Create] a new Spring Boot project and add the `spring-ai-vertex-ai-palm2-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 VertexAi chat model:
[source,application.properties]
----
spring.ai.vertex.ai.api-key=YOUR_API_KEY
spring.ai.vertex.ai.chat.model=chat-bison-001
spring.ai.vertex.ai.chat.options.temperature=0.5
----
TIP: replace the `api-key` with your VertexAI credentials.
This will create a `VertexAiPaLm2ChatModel` 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 VertexAiPaLm2ChatModel chatModel;
@Autowired
public ChatController(VertexAiPaLm2ChatModel 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<ChatResponse> generateStream(@RequestParam(value = "message", defaultValue = "Tell me a joke") String message) {
Prompt prompt = new Prompt(new UserMessage(message));
return chatModel.stream(prompt);
}
}
----
== Manual Configuration
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-openai/src/main/java/org/springframework/ai/vertexai/paml2/VertexAiPaLm2ChatModel.java[VertexAiPaLm2ChatModel] implements the `ChatModel` and uses the <<low-level-api>> to connect to the VertexAI service.
Add the `spring-ai-vertex-ai-palm2` dependency to your project's Maven `pom.xml` file:
[source, xml]
----
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-vertex-ai-palm2</artifactId>
</dependency>
----
or to your Gradle `build.gradle` build file.
[source,groovy]
----
dependencies {
implementation 'org.springframework.ai:spring-ai-vertex-ai-palm'
}
----
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 `VertexAiPaLm2ChatModel` and use it for text generations:
[source,java]
----
VertexAiPaLm2Api vertexAiApi = new VertexAiPaLm2Api(< YOUR PALM_API_KEY>);
var chatModel = new VertexAiPaLm2ChatModel(vertexAiApi,
VertexAiPaLm2ChatOptions.builder()
.withTemperature(0.4)
.build());
ChatResponse response = chatModel.call(
new Prompt("Generate the names of 5 famous pirates."));
----
The `VertexAiPaLm2ChatOptions` provides the configuration information for the chat requests.
The `VertexAiPaLm2ChatOptions.Builder` is fluent options builder.
=== Low-level VertexAiPaLm2Api Client [[low-level-api]]
The https://github.com/spring-projects/spring-ai/blob/main/models/spring-ai-vertex-aipalm2/src/main/java/org/springframework/ai/vertexai/palm2/api/VertexAiPaLm2Api.java[VertexAiPaLm2Api] provides is lightweight Java client for VertexAiPaLm2Api Chat API.
Following class diagram illustrates the `VertexAiPaLm2Api` chat interfaces and building blocks:
image::vertex-ai-chat-low-level-api.jpg[w=800,align="center"]
Here is a simple snippet how to use the api programmatically:
[source,java]
----
VertexAiPaLm2Api vertexAiApi = new VertexAiPaLm2Api(< YOUR PALM_API_KEY>);
// Generate
var prompt = new MessagePrompt(List.of(new Message("0", "Hello, how are you?")));
GenerateMessageRequest request = new GenerateMessageRequest(prompt);
GenerateMessageResponse response = vertexAiApi.generateMessage(request);
// Embed text
Embedding embedding = vertexAiApi.embedText("Hello, how are you?");
// Batch embedding
List<Embedding> embeddings = vertexAiApi.batchEmbedText(List.of("Hello, how are you?", "I am fine, thank you!"));
----

View File

@@ -1,68 +0,0 @@
/*
* 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.vertexai.palm2;
import org.springframework.ai.autoconfigure.retry.SpringAiRetryAutoConfiguration;
import org.springframework.ai.vertexai.palm2.VertexAiPaLm2ChatModel;
import org.springframework.ai.vertexai.palm2.VertexAiPaLm2EmbeddingModel;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
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.autoconfigure.web.reactive.function.client.WebClientAutoConfiguration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestClient;
@AutoConfiguration(after = RestClientAutoConfiguration.class)
@ConditionalOnClass(VertexAiPaLm2Api.class)
@EnableConfigurationProperties({ VertexAiPalm2ConnectionProperties.class, VertexAiPlam2ChatProperties.class,
VertexAiPalm2EmbeddingProperties.class })
@ImportAutoConfiguration(classes = { SpringAiRetryAutoConfiguration.class, RestClientAutoConfiguration.class,
WebClientAutoConfiguration.class })
public class VertexAiPalm2AutoConfiguration {
@Bean
@ConditionalOnMissingBean
public VertexAiPaLm2Api vertexAiApi(VertexAiPalm2ConnectionProperties connectionProperties,
VertexAiPalm2EmbeddingProperties embeddingAiProperties, VertexAiPlam2ChatProperties chatProperties,
RestClient.Builder restClientBuilder) {
return new VertexAiPaLm2Api(connectionProperties.getBaseUrl(), connectionProperties.getApiKey(),
chatProperties.getModel(), embeddingAiProperties.getModel(), restClientBuilder);
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = VertexAiPlam2ChatProperties.CONFIG_PREFIX, name = "enabled", havingValue = "true",
matchIfMissing = true)
public VertexAiPaLm2ChatModel vertexAiChatModel(VertexAiPaLm2Api vertexAiApi,
VertexAiPlam2ChatProperties chatProperties) {
return new VertexAiPaLm2ChatModel(vertexAiApi, chatProperties.getOptions());
}
@Bean
@ConditionalOnMissingBean
@ConditionalOnProperty(prefix = VertexAiPalm2EmbeddingProperties.CONFIG_PREFIX, name = "enabled",
havingValue = "true", matchIfMissing = true)
public VertexAiPaLm2EmbeddingModel vertexAiEmbeddingModel(VertexAiPaLm2Api vertexAiApi) {
return new VertexAiPaLm2EmbeddingModel(vertexAiApi);
}
}

View File

@@ -1,53 +0,0 @@
/*
* 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.vertexai.palm2;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(VertexAiPalm2ConnectionProperties.CONFIG_PREFIX)
public class VertexAiPalm2ConnectionProperties {
public static final String CONFIG_PREFIX = "spring.ai.vertex.ai";
/**
* Vertex AI PaLM API access key.
*/
private String apiKey;
/**
* Vertex AI PaLM API base URL. Defaults to
* https://generativelanguage.googleapis.com/v1beta3
*/
private String baseUrl = VertexAiPaLm2Api.DEFAULT_BASE_URL;
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;
}
}

View File

@@ -1,52 +0,0 @@
/*
* 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.vertexai.palm2;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(VertexAiPalm2EmbeddingProperties.CONFIG_PREFIX)
public class VertexAiPalm2EmbeddingProperties {
public static final String CONFIG_PREFIX = "spring.ai.vertex.ai.embedding";
/**
* Enable Vertex AI PaLM API embedding model.
*/
private boolean enabled = true;
/**
* Vertex AI PaLM API embedding generative name. Defaults to embedding-gecko-001.
*/
private String model = VertexAiPaLm2Api.DEFAULT_EMBEDDING_MODEL;
public String getModel() {
return this.model;
}
public void setModel(String model) {
this.model = model;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@@ -1,71 +0,0 @@
/*
* 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.vertexai.palm2;
import org.springframework.ai.vertexai.palm2.VertexAiPaLm2ChatOptions;
import org.springframework.ai.vertexai.palm2.api.VertexAiPaLm2Api;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(VertexAiPlam2ChatProperties.CONFIG_PREFIX)
public class VertexAiPlam2ChatProperties {
public static final String CONFIG_PREFIX = "spring.ai.vertex.ai.chat";
/**
* Enable Vertex AI PaLM API chat model
*/
private boolean enabled = true;
/**
* Vertex AI PaLM API generative name. Defaults to chat-bison-001
*/
private String model = VertexAiPaLm2Api.DEFAULT_GENERATE_MODEL;
/**
* Vertex AI PaLM API generative options.
*/
private VertexAiPaLm2ChatOptions options = VertexAiPaLm2ChatOptions.builder()
.withTemperature(0.7f)
.withTopP(null)
.withCandidateCount(1)
.withTopK(20)
.build();
public String getModel() {
return this.model;
}
public void setModel(String model) {
this.model = model;
}
public VertexAiPaLm2ChatOptions getOptions() {
return this.options;
}
public void setOptions(VertexAiPaLm2ChatOptions options) {
this.options = options;
}
public boolean isEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}

View File

@@ -1,118 +0,0 @@
/*
* 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.vertexai.palm2;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
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.embedding.EmbeddingResponse;
import org.springframework.ai.vertexai.palm2.VertexAiPaLm2ChatModel;
import org.springframework.ai.vertexai.palm2.VertexAiPaLm2EmbeddingModel;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
// NOTE: works only with US location. Use VPN if you are outside US.
@EnabledIfEnvironmentVariable(named = "PALM_API_KEY", matches = ".*")
public class VertexAiPaLm2AutoConfigurationIT {
private static final Log logger = LogFactory.getLog(VertexAiPaLm2AutoConfigurationIT.class);
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
.withPropertyValues("spring.ai.vertex.ai.baseUrl=https://generativelanguage.googleapis.com/v1beta3",
"spring.ai.vertex.ai.apiKey=" + System.getenv("PALM_API_KEY"),
"spring.ai.vertex.ai.chat.model=chat-bison-001", "spring.ai.vertex.ai.chat.options.temperature=0.8",
"spring.ai.vertex.ai.embedding.model=embedding-gecko-001")
.withConfiguration(AutoConfigurations.of(VertexAiPalm2AutoConfiguration.class));
@Test
void generate() {
contextRunner.run(context -> {
VertexAiPaLm2ChatModel chatModel = context.getBean(VertexAiPaLm2ChatModel.class);
String response = chatModel.call("Hello");
assertThat(response).isNotEmpty();
logger.info("Response: " + response);
});
}
@Test
void embedding() {
contextRunner.run(context -> {
VertexAiPaLm2EmbeddingModel embeddingModel = context.getBean(VertexAiPaLm2EmbeddingModel.class);
EmbeddingResponse embeddingResponse = embeddingModel
.embedForResponse(List.of("Hello World", "World is big and salvation is near"));
assertThat(embeddingResponse.getResults()).hasSize(2);
assertThat(embeddingResponse.getResults().get(0).getOutput()).isNotEmpty();
assertThat(embeddingResponse.getResults().get(0).getIndex()).isEqualTo(0);
assertThat(embeddingResponse.getResults().get(1).getOutput()).isNotEmpty();
assertThat(embeddingResponse.getResults().get(1).getIndex()).isEqualTo(1);
assertThat(embeddingModel.dimensions()).isEqualTo(768);
});
}
@Test
public void embeddingActivation() {
// Disable the embedding auto-configuration.
contextRunner.withPropertyValues("spring.ai.vertex.ai.embedding.enabled=false").run(context -> {
assertThat(context.getBeansOfType(VertexAiPalm2EmbeddingProperties.class)).isNotEmpty();
assertThat(context.getBeansOfType(VertexAiPaLm2EmbeddingModel.class)).isEmpty();
});
// The embedding auto-configuration is enabled by default.
contextRunner.run(context -> {
assertThat(context.getBeansOfType(VertexAiPalm2EmbeddingProperties.class)).isNotEmpty();
assertThat(context.getBeansOfType(VertexAiPaLm2EmbeddingModel.class)).isNotEmpty();
});
// Explicitly enable the embedding auto-configuration.
contextRunner.withPropertyValues("spring.ai.vertex.ai.embedding.enabled=true").run(context -> {
assertThat(context.getBeansOfType(VertexAiPalm2EmbeddingProperties.class)).isNotEmpty();
assertThat(context.getBeansOfType(VertexAiPaLm2EmbeddingModel.class)).isNotEmpty();
});
}
@Test
public void chatActivation() {
// Disable the chat auto-configuration.
contextRunner.withPropertyValues("spring.ai.vertex.ai.chat.enabled=false").run(context -> {
assertThat(context.getBeansOfType(VertexAiPlam2ChatProperties.class)).isNotEmpty();
assertThat(context.getBeansOfType(VertexAiPaLm2ChatModel.class)).isEmpty();
});
// The chat auto-configuration is enabled by default.
contextRunner.run(context -> {
assertThat(context.getBeansOfType(VertexAiPlam2ChatProperties.class)).isNotEmpty();
assertThat(context.getBeansOfType(VertexAiPaLm2ChatModel.class)).isNotEmpty();
});
// Explicitly enable the chat auto-configuration.
contextRunner.withPropertyValues("spring.ai.vertex.ai.chat.enabled=true").run(context -> {
assertThat(context.getBeansOfType(VertexAiPlam2ChatProperties.class)).isNotEmpty();
assertThat(context.getBeansOfType(VertexAiPaLm2ChatModel.class)).isNotEmpty();
});
}
}

View File

@@ -1,42 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai</artifactId>
<version>1.0.0-SNAPSHOT</version>
<relativePath>../../pom.xml</relativePath>
</parent>
<artifactId>spring-ai-vertex-ai-palm2-spring-boot-starter</artifactId>
<packaging>jar</packaging>
<name>Spring AI Starter - VertexAI PaLM2</name>
<description>Spring AI VertexAI PaLM2 Auto Configuration</description>
<url>https://github.com/spring-projects/spring-ai</url>
<scm>
<url>https://github.com/spring-projects/spring-ai</url>
<connection>git://github.com/spring-projects/spring-ai.git</connection>
<developerConnection>git@github.com:spring-projects/spring-ai.git</developerConnection>
</scm>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-spring-boot-autoconfigure</artifactId>
<version>${project.parent.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-vertex-ai-palm2</artifactId>
<version>${project.parent.version}</version>
</dependency>
</dependencies>
</project>

View File

@@ -34,13 +34,6 @@
</dependency>
<!-- TESTING -->
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-vertex-ai-palm2</artifactId>
<version>${parent.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-openai</artifactId>