Skip to content

Commit

Permalink
Bump dashscope-sdk-java from 2.13.0 to 2.14.4 (langchain4j#1189)
Browse files Browse the repository at this point in the history
## Issue
<!-- Please paste the link to the issue this PR is addressing. For
example: langchain4j#1012 -->


## Change
<!-- Please describe the changes you made. -->


## General checklist
<!-- Please double-check the following points and mark them like this:
[X] -->
- [x] There are no breaking changes
- [x] I have added unit and integration tests for my change
- [x] I have manually run all the unit and integration tests in the
module I have added/changed, and they are all green
- [ ] I have manually run all the unit and integration tests in the
[core](https://github.com/langchain4j/langchain4j/tree/main/langchain4j-core)
and
[main](https://github.com/langchain4j/langchain4j/tree/main/langchain4j)
modules, and they are all green
<!-- Before adding documentation and example(s) (below), please wait
until the PR is reviewed and approved. -->
- [ ] I have added/updated the
[documentation](https://github.com/langchain4j/langchain4j/tree/main/docs/docs)
- [ ] I have added an example in the [examples
repo](https://github.com/langchain4j/langchain4j-examples) (only for
"big" features)


## Checklist for adding new model integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added my new module in the
[BOM](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-bom/pom.xml)


## Checklist for adding new embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have added a `{NameOfIntegration}EmbeddingStoreIT` that extends
from either `EmbeddingStoreIT` or `EmbeddingStoreWithFilteringIT`
- [ ] I have added my new module in the
[BOM](https://github.com/langchain4j/langchain4j/blob/main/langchain4j-bom/pom.xml)


## Checklist for changing existing embedding store integration
<!-- Please double-check the following points and mark them like this:
[X] -->
- [ ] I have manually verified that the
`{NameOfIntegration}EmbeddingStore` works correctly with the data
persisted using the latest released version of LangChain4j
  • Loading branch information
jiangsier-xyz authored May 28, 2024
1 parent 8eccd34 commit 667602f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 15 deletions.
2 changes: 1 addition & 1 deletion langchain4j-dashscope/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>dashscope-sdk-java</artifactId>
<version>2.13.0</version>
<version>2.14.4</version>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.langchain4j.model.dashscope;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
Expand All @@ -24,6 +24,10 @@
import static dev.langchain4j.model.dashscope.QwenHelper.*;
import static dev.langchain4j.spi.ServiceHelper.loadFactories;

/**
* Represents a Qwen language model with a chat completion interface.
* More details are available <a href="https://help.aliyun.com/zh/dashscope/developer-reference/api-details">here</a>.
*/
public class QwenChatModel implements ChatLanguageModel {
private final String apiKey;
private final String modelName;
Expand Down Expand Up @@ -85,7 +89,7 @@ public Response<AiMessage> generate(List<ChatMessage> messages) {

private Response<AiMessage> generateByNonMultimodalModel(List<ChatMessage> messages) {
try {
QwenParam.QwenParamBuilder<?, ?> builder = QwenParam.builder()
GenerationParam.GenerationParamBuilder<?, ?> builder = GenerationParam.builder()
.apiKey(apiKey)
.model(modelName)
.topP(topP)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
import static dev.langchain4j.spi.ServiceHelper.loadFactories;
import static java.util.Collections.singletonList;

/**
* An implementation of an {@link EmbeddingModel} that uses
* <a href="https://help.aliyun.com/zh/dashscope/developer-reference/text-embedding-api-details">DashScope Embeddings API</a>.
*/
public class QwenEmbeddingModel implements EmbeddingModel {

public static final String TYPE_KEY = "type";
Expand All @@ -43,14 +47,14 @@ public QwenEmbeddingModel(String apiKey, String modelName) {
private boolean containsDocuments(List<TextSegment> textSegments) {
return textSegments.stream()
.map(TextSegment::metadata)
.map(metadata -> metadata.get(TYPE_KEY))
.map(metadata -> metadata.getString(TYPE_KEY))
.anyMatch(TYPE_DOCUMENT::equalsIgnoreCase);
}

private boolean containsQueries(List<TextSegment> textSegments) {
return textSegments.stream()
.map(TextSegment::metadata)
.map(metadata -> metadata.get(TYPE_KEY))
.map(metadata -> metadata.getString(TYPE_KEY))
.anyMatch(TYPE_QUERY::equalsIgnoreCase);
}

Expand Down Expand Up @@ -123,7 +127,7 @@ public Response<List<Embedding>> embedAll(List<TextSegment> textSegments) {
Integer tokens = null;
for (TextSegment textSegment : textSegments) {
Response<List<Embedding>> result;
if (TYPE_QUERY.equalsIgnoreCase(textSegment.metadata(TYPE_KEY))) {
if (TYPE_QUERY.equalsIgnoreCase(textSegment.metadata().getString(TYPE_KEY))) {
result = embedTexts(singletonList(textSegment), QUERY);
} else {
result = embedTexts(singletonList(textSegment), DOCUMENT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.langchain4j.model.dashscope;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.protocol.Protocol;
Expand All @@ -20,6 +20,10 @@
import static dev.langchain4j.model.dashscope.QwenModelName.QWEN_PLUS;
import static dev.langchain4j.spi.ServiceHelper.loadFactories;

/**
* Represents a Qwen language model with a text interface.
* More details are available <a href="https://help.aliyun.com/zh/dashscope/developer-reference/api-details">here</a>.
*/
public class QwenLanguageModel implements LanguageModel {
private final String apiKey;
private final String modelName;
Expand Down Expand Up @@ -71,7 +75,7 @@ public QwenLanguageModel(String baseUrl,
@Override
public Response<String> generate(String prompt) {
try {
QwenParam.QwenParamBuilder<?, ?> builder = QwenParam.builder()
GenerationParam.GenerationParamBuilder<?, ?> builder = GenerationParam.builder()
.apiKey(apiKey)
.model(modelName)
.topP(topP)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.langchain4j.model.dashscope;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversation;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationParam;
import com.alibaba.dashscope.aigc.multimodalconversation.MultiModalConversationResult;
Expand All @@ -26,6 +26,12 @@
import static dev.langchain4j.model.dashscope.QwenHelper.toQwenMultiModalMessages;
import static dev.langchain4j.spi.ServiceHelper.loadFactories;

/**
* Represents a Qwen language model with a chat completion interface.
* The model's response is streamed token by token and should be handled with {@link StreamingResponseHandler}.
* <br>
* More details are available <a href="https://help.aliyun.com/zh/dashscope/developer-reference/api-details">here</a>
*/
public class QwenStreamingChatModel implements StreamingChatLanguageModel {
private final String apiKey;
private final String modelName;
Expand Down Expand Up @@ -91,7 +97,7 @@ public void generate(List<ChatMessage> messages, StreamingResponseHandler<AiMess

private void generateByNonMultimodalModel(List<ChatMessage> messages, StreamingResponseHandler<AiMessage> handler) {
try {
QwenParam.QwenParamBuilder<?, ?> builder = QwenParam.builder()
GenerationParam.GenerationParamBuilder<?, ?> builder = GenerationParam.builder()
.apiKey(apiKey)
.model(modelName)
.topP(topP)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package dev.langchain4j.model.dashscope;

import com.alibaba.dashscope.aigc.generation.Generation;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.aigc.generation.GenerationResult;
import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.common.ResultCallback;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
Expand All @@ -22,6 +22,12 @@
import static dev.langchain4j.model.dashscope.QwenModelName.QWEN_PLUS;
import static dev.langchain4j.spi.ServiceHelper.loadFactories;

/**
* Represents a Qwen language model with a text interface.
* The model's response is streamed token by token and should be handled with {@link StreamingResponseHandler}.
* <br>
* More details are available <a href="https://help.aliyun.com/zh/dashscope/developer-reference/api-details">here</a>.
*/
public class QwenStreamingLanguageModel implements StreamingLanguageModel {
private final String apiKey;
private final String modelName;
Expand Down Expand Up @@ -73,7 +79,7 @@ public QwenStreamingLanguageModel(String baseUrl,
@Override
public void generate(String prompt, StreamingResponseHandler<String> handler) {
try {
QwenParam.QwenParamBuilder<?, ?> builder = QwenParam.builder()
GenerationParam.GenerationParamBuilder<?, ?> builder = GenerationParam.builder()
.apiKey(apiKey)
.model(modelName)
.topP(topP)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package dev.langchain4j.model.dashscope;

import com.alibaba.dashscope.aigc.generation.models.QwenParam;
import com.alibaba.dashscope.aigc.generation.GenerationParam;
import com.alibaba.dashscope.exception.InputRequiredException;
import com.alibaba.dashscope.exception.NoApiKeyException;
import com.alibaba.dashscope.tokenizers.Tokenization;
Expand Down Expand Up @@ -36,7 +36,7 @@ public QwenTokenizer(String apiKey, String modelName) {
public int estimateTokenCountInText(String text) {
String prompt = isBlank(text) ? text + "_" : text;
try {
QwenParam param = QwenParam.builder()
GenerationParam param = GenerationParam.builder()
.apiKey(apiKey)
.model(modelName)
.prompt(prompt)
Expand All @@ -58,7 +58,7 @@ public int estimateTokenCountInMessage(ChatMessage message) {
@Override
public int estimateTokenCountInMessages(Iterable<ChatMessage> messages) {
try {
QwenParam param = QwenParam.builder()
GenerationParam param = GenerationParam.builder()
.apiKey(apiKey)
.model(modelName)
.messages(toQwenMessages(messages))
Expand Down

0 comments on commit 667602f

Please sign in to comment.