Skip to content

Commit

Permalink
docs: add reranker & proxy example (#564)
Browse files Browse the repository at this point in the history
  • Loading branch information
Azure99 authored May 30, 2024
1 parent 30f069b commit fe734d0
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 4 deletions.
26 changes: 23 additions & 3 deletions java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>qianfan</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
</dependency>
```

Expand All @@ -21,13 +21,13 @@
对于Kotlin DSL,在build.gradle.kts的dependencies中添加依赖

```kotlin
implementation("com.baidubce:qianfan:0.0.5")
implementation("com.baidubce:qianfan:0.0.6")
```

对于Groovy DSL,在build.gradle的dependencies中添加依赖

```groovy
implementation 'com.baidubce:qianfan:0.0.5'
implementation 'com.baidubce:qianfan:0.0.6'
```

> 我们提供了一些 [示例](./examples),可以帮助快速了解 SDK 的使用方法并完成常见功能。
Expand Down Expand Up @@ -161,3 +161,23 @@ Image2TextResponse response = new Qianfan().image2Text()
.execute();
System.out.println(response.getResult());
```

### Rerank

千帆 SDK 支持调用Rerank模型,用于重新排序向量模型返回的top-k文档,优化语义搜索结果。

```java
List<String> documents = new ArrayList<>();
documents.add("上海位于中国东部海岸线的中心,长江三角洲最东部。");
documents.add("上海现在的温度是27度。");
documents.add("深圳现在的温度是29度。");

RerankResponse response = new Qianfan().rerank()
.query("上海现在气温多少?")
.documents(documents)
.execute();
response.getResults().forEach(data -> {
System.out.println(data.getDocument());
System.out.println(data.getRelevanceScore());
});
```
2 changes: 1 addition & 1 deletion java/example/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<dependency>
<groupId>com.baidubce</groupId>
<artifactId>qianfan</artifactId>
<version>0.0.5</version>
<version>0.0.6</version>
</dependency>
</dependencies>
</project>
19 changes: 19 additions & 0 deletions java/example/src/main/java/com/baidubce/HttpProxyExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.baidubce;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.chat.ChatResponse;

/**
* 本示例通过设置系统代理,实现了SDK的请求通过代理服务器发送
*/
public class HttpProxyExample {
public static void main(String[] args) {
System.setProperty("https.proxyHost", "127.0.0.1");
System.setProperty("https.proxyPort", "7890");
Qianfan qianfan = new Qianfan();
ChatResponse response = qianfan.chatCompletion()
.addMessage("user", "你好!我正在通过代理与你聊天哦")
.execute();
System.out.println(response.getResult());
}
}
28 changes: 28 additions & 0 deletions java/example/src/main/java/com/baidubce/RerankExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.baidubce;

import com.baidubce.qianfan.Qianfan;
import com.baidubce.qianfan.model.rerank.RerankResponse;

import java.util.ArrayList;
import java.util.List;

/**
* 本示例实现了Reranker调用流程,对于给定的Query和文档列表进行重排序,将更相关的文档排在前面
*/
public class RerankExample {
public static void main(String[] args) {
List<String> documents = new ArrayList<>();
documents.add("上海位于中国东部海岸线的中心,长江三角洲最东部。");
documents.add("上海现在的温度是27度。");
documents.add("深圳现在的温度是29度。");

RerankResponse response = new Qianfan().rerank()
.query("上海现在气温多少?")
.documents(documents)
.execute();
response.getResults().forEach(data -> {
System.out.println(data.getDocument());
System.out.println(data.getRelevanceScore());
});
}
}

0 comments on commit fe734d0

Please sign in to comment.