Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Zhipuai Support Vector Model, Model Number: embedding-3 Document addr…
…ess: https://bigmodel.cn/dev/api/vector/embedding-3

Zhipuai Support Vector Model, Model Number: embedding-3
Document address: https://bigmodel.cn/dev/api/vector/embedding-3
  • Loading branch information
yp committed Dec 6, 2024
commit 0245cc6d7a75a550770e717412baae4ae6d5d4a3
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,12 @@ public enum EmbeddingModel {
/**
* DIMENSION: 1024
*/
Embedding_2("Embedding-2");
Embedding_2("Embedding-2"),

/**
* DIMENSION: 2048
*/
Embedding_3("Embedding-3");

public final String value;

Expand Down Expand Up @@ -960,13 +965,20 @@ public record EmbeddingRequest<T>(

@JsonProperty("dimensions") Integer dimensions) {


/**
* Create an embedding request with the given input. Encoding model is set to 'embedding-2'.
* @param input Input text to embed.
*/
public EmbeddingRequest(T input) {
this(input,DEFAULT_EMBEDDING_MODEL,null);
}

/**
* Create an embedding request with the given input. Encoding model is set to 'embedding-3'.
* @param input Input text to embed.
*/
public EmbeddingRequest(T input, Integer dimensions) {
this(input,DEFAULT_EMBEDDING_MODEL,dimensions);
this(input,EmbeddingModel.Embedding_3.getValue(),dimensions);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.zhipuai;

import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingModel;
import org.springframework.ai.zhipuai.api.ZhiPuAiApi;
import org.springframework.ai.zhipuai.api.ZhiPuAiImageApi;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;

/**
* @author Yee Pan
*/
@SpringBootConfiguration
public class ZhiPuAiEmbeddingModleTestConfiguration {

@Bean
public ZhiPuAiApi zhiPuAiApi() {
return new ZhiPuAiApi(getApiKey());
}

@Bean
public ZhiPuAiImageApi zhiPuAiImageApi() {
return new ZhiPuAiImageApi(getApiKey());
}

private String getApiKey() {
String apiKey = System.getenv("ZHIPU_AI_API_KEY");
if (!StringUtils.hasText(apiKey)) {
throw new IllegalArgumentException(
"You must provide an API key. Put it in an environment variable under the name ZHIPU_AI_API_KEY");
}
return apiKey;
}

@Bean
public EmbeddingModel zhiPuAiEmbeddingModel(ZhiPuAiApi api) {
return new ZhiPuAiEmbeddingModel(api,MetadataMode.EMBED,ZhiPuAiEmbeddingOptions.builder().withModel(ZhiPuAiApi.EmbeddingModel.Embedding_3.getValue()).withDimensions(1024).build());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,17 @@ void chatCompletionStream() {
@Test
void embeddings() {
ResponseEntity<EmbeddingList<Embedding>> response = this.zhiPuAiApi
.embeddings(new ZhiPuAiApi.EmbeddingRequest<>("Hello world", ZhiPuAiApi.DEFAULT_EMBEDDING_MODEL, 1024));
.embeddings(new ZhiPuAiApi.EmbeddingRequest<>("Hello world"));

assertThat(response).isNotNull();
assertThat(Objects.requireNonNull(response.getBody()).data()).hasSize(1);
assertThat(response.getBody().data().get(0).embedding()).hasSize(1024);
}

@Test
void embeddings3() {
ResponseEntity<EmbeddingList<Embedding>> response = this.zhiPuAiApi
.embeddings(new ZhiPuAiApi.EmbeddingRequest<>("Hello world",1024));

assertThat(response).isNotNull();
assertThat(Objects.requireNonNull(response.getBody()).data()).hasSize(1);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* 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.zhipuai.embedding3;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.zhipuai.ZhiPuAiEmbeddingModel;
import org.springframework.ai.zhipuai.ZhiPuAiEmbeddingModleTestConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;

/**
* <a href="https://bigmodel.cn/dev/api/vector/embedding-3">zhipuai vector model embedding-3 doc</a>
*
* @author Yee Pan
*/
@SpringBootTest(classes = ZhiPuAiEmbeddingModleTestConfiguration.class)
@EnabledIfEnvironmentVariable(named = "ZHIPU_AI_API_KEY", matches = ".+")
class Embedding3IT {

@Autowired
private ZhiPuAiEmbeddingModel embeddingModel;

@Test
void defaultEmbedding() {
assertThat(this.embeddingModel).isNotNull();

EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of("Hello World"));

assertThat(embeddingResponse.getResults()).hasSize(1);

assertThat(embeddingResponse.getResults().get(0)).isNotNull();
assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(1024);

assertThat(this.embeddingModel.dimensions()).isEqualTo(1024);
}

@Test
void batchEmbedding() {
assertThat(this.embeddingModel).isNotNull();

EmbeddingResponse embeddingResponse = this.embeddingModel.embedForResponse(List.of("Hello World", "HI"));

assertThat(embeddingResponse.getResults()).hasSize(2);

assertThat(embeddingResponse.getResults().get(0)).isNotNull();
assertThat(embeddingResponse.getResults().get(0).getOutput()).hasSize(1024);

assertThat(embeddingResponse.getResults().get(1)).isNotNull();
assertThat(embeddingResponse.getResults().get(1).getOutput()).hasSize(1024);

assertThat(this.embeddingModel.dimensions()).isEqualTo(1024);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* 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.zhipuai.embedding3;

import io.micrometer.observation.tck.TestObservationRegistry;
import io.micrometer.observation.tck.TestObservationRegistryAssert;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable;
import org.springframework.ai.document.MetadataMode;
import org.springframework.ai.embedding.EmbeddingRequest;
import org.springframework.ai.embedding.EmbeddingResponse;
import org.springframework.ai.embedding.EmbeddingResponseMetadata;
import org.springframework.ai.embedding.observation.DefaultEmbeddingModelObservationConvention;
import org.springframework.ai.observation.conventions.AiOperationType;
import org.springframework.ai.observation.conventions.AiProvider;
import org.springframework.ai.zhipuai.ZhiPuAiEmbeddingModel;
import org.springframework.ai.zhipuai.ZhiPuAiEmbeddingOptions;
import org.springframework.ai.zhipuai.api.ZhiPuAiApi;
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 org.springframework.retry.support.RetryTemplate;

import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.ai.embedding.observation.EmbeddingModelObservationDocumentation.HighCardinalityKeyNames;
import static org.springframework.ai.embedding.observation.EmbeddingModelObservationDocumentation.LowCardinalityKeyNames;

/**
* Integration tests for observation instrumentation in {@link ZhiPuAiEmbeddingModel}.
* <a href="https://bigmodel.cn/dev/api/vector/embedding-3">zhipuai vector model embedding-3 doc</a>.
*
* @author Yee Pan
*/
@SpringBootTest(classes = ZhiPuAiEmbedding3ModelObservationIT.Config.class)
@EnabledIfEnvironmentVariable(named = "ZHIPU_AI_API_KEY", matches = ".+")
public class ZhiPuAiEmbedding3ModelObservationIT {

@Autowired
TestObservationRegistry observationRegistry;

@Autowired
ZhiPuAiEmbeddingModel embeddingModel;

@Test
void observationForEmbeddingOperation() {
var options = ZhiPuAiEmbeddingOptions.builder()
.withModel(ZhiPuAiApi.EmbeddingModel.Embedding_3.getValue())
.build();

EmbeddingRequest embeddingRequest = new EmbeddingRequest(List.of("Here comes the sun"), options);

EmbeddingResponse embeddingResponse = this.embeddingModel.call(embeddingRequest);
assertThat(embeddingResponse.getResults()).isNotEmpty();

EmbeddingResponseMetadata responseMetadata = embeddingResponse.getMetadata();
assertThat(responseMetadata).isNotNull();

TestObservationRegistryAssert.assertThat(this.observationRegistry)
.doesNotHaveAnyRemainingCurrentObservation()
.hasObservationWithNameEqualTo(DefaultEmbeddingModelObservationConvention.DEFAULT_NAME)
.that()
.hasContextualNameEqualTo("embedding " + ZhiPuAiApi.EmbeddingModel.Embedding_3.getValue())
.hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_OPERATION_TYPE.asString(),
AiOperationType.EMBEDDING.value())
.hasLowCardinalityKeyValue(LowCardinalityKeyNames.AI_PROVIDER.asString(), AiProvider.ZHIPUAI.value())
.hasLowCardinalityKeyValue(LowCardinalityKeyNames.REQUEST_MODEL.asString(),
ZhiPuAiApi.EmbeddingModel.Embedding_3.getValue())
.hasLowCardinalityKeyValue(LowCardinalityKeyNames.RESPONSE_MODEL.asString(), responseMetadata.getModel())
.hasHighCardinalityKeyValue(HighCardinalityKeyNames.USAGE_INPUT_TOKENS.asString(),
String.valueOf(responseMetadata.getUsage().getPromptTokens()))
.hasHighCardinalityKeyValue(HighCardinalityKeyNames.USAGE_TOTAL_TOKENS.asString(),
String.valueOf(responseMetadata.getUsage().getTotalTokens()))
.hasBeenStarted()
.hasBeenStopped();
}

@SpringBootConfiguration
static class Config {

@Bean
public TestObservationRegistry observationRegistry() {
return TestObservationRegistry.create();
}

@Bean
public ZhiPuAiApi zhiPuAiApi() {
return new ZhiPuAiApi(System.getenv("ZHIPU_AI_API_KEY"));
}

@Bean
public ZhiPuAiEmbeddingModel zhiPuAiEmbeddingModel(ZhiPuAiApi zhiPuAiApi,
TestObservationRegistry observationRegistry) {
return new ZhiPuAiEmbeddingModel(zhiPuAiApi, MetadataMode.EMBED,
ZhiPuAiEmbeddingOptions.builder().withModel(ZhiPuAiApi.EmbeddingModel.Embedding_3.getValue()).build(),
RetryTemplate.defaultInstance(), observationRegistry);
}

}

}