stop;
71 |
72 | /**
73 | * 使用什么采样温度,介于 0 和 2 之间,默认值为 1
74 | * 较高的值(如 0.8)将使输出更加随机,而较低的值(如 0.2)将使其更具集中性和确定性
75 | */
76 | private double temperature;
77 |
78 | /**
79 | * 默认值为 1
80 | * 温度采样的替代方法,称为核采样,其中模型考虑具有top_p概率质量的标记的结果。因此,0.1 表示仅考虑包含前 10% 概率质量的代币。
81 | */
82 | @JsonProperty("top_p")
83 | private Double topP;
84 |
85 | /**
86 | * 调用标识,避免重复调用
87 | */
88 | private String user;
89 |
90 | @Getter
91 | @AllArgsConstructor
92 | public enum Model {
93 | GPT_3_5_TURBO("gpt-3.5-turbo"), GPT_4("gpt-4"), GPT_4_32K("gpt-4-32k"),
94 | GPT_4_VISION_PREVIEW("gpt-4-vision-preview"),
95 | ;
96 | private String moduleName;
97 | }
98 |
99 | }
100 |
--------------------------------------------------------------------------------
/smartFuse-openai/src/main/java/com/ai/openai/parameter/input/OpenaiEmbeddingParameter.java:
--------------------------------------------------------------------------------
1 | package com.ai.openai.parameter.input;
2 |
3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4 | import com.fasterxml.jackson.annotation.JsonInclude;
5 | import com.fasterxml.jackson.annotation.JsonProperty;
6 | import lombok.*;
7 |
8 | import java.io.Serializable;
9 |
10 | @Data
11 | @Builder
12 | @NoArgsConstructor
13 | @AllArgsConstructor
14 | @JsonIgnoreProperties(ignoreUnknown = true)
15 | @JsonInclude(JsonInclude.Include.NON_NULL)
16 | public class OpenaiEmbeddingParameter implements Serializable {
17 |
18 | /**
19 | * 要使用的模型的 ID。
20 | */
21 | @Builder.Default
22 | private String model = OpenaiEmbeddingParameter.Model.TEXT_EMBEDDING_ADA_002.getModelName();
23 |
24 | /**
25 | * 要返回嵌入的格式。
26 | */
27 | @JsonProperty("encoding_format")
28 | private String encodingFormat;
29 |
30 | @Getter
31 | @AllArgsConstructor
32 | public enum Model {
33 | TEXT_EMBEDDING_ADA_002("text-embedding-ada-002"),
34 | ;
35 | private final String modelName;
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/smartFuse-openai/src/main/java/com/ai/openai/parameter/input/OpenaiImageParameter.java:
--------------------------------------------------------------------------------
1 | package com.ai.openai.parameter.input;
2 |
3 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
4 | import com.fasterxml.jackson.annotation.JsonInclude;
5 | import com.fasterxml.jackson.annotation.JsonProperty;
6 | import lombok.*;
7 |
8 | import java.io.Serializable;
9 |
10 | @Data
11 | @Builder
12 | @NoArgsConstructor
13 | @AllArgsConstructor
14 | @JsonIgnoreProperties(ignoreUnknown = true)
15 | @JsonInclude(JsonInclude.Include.NON_NULL)
16 | public class OpenaiImageParameter implements Serializable {
17 |
18 | /**
19 | * 用于图像生成的模型
20 | *
21 | * 默认为 dall-e-2
22 | */
23 | @Builder.Default
24 | private String model = OpenaiImageParameter.Model.DALL_E_3.getName();
25 |
26 | /**
27 | * 将生成的图像的质量。 创建具有更精细细节和更高一致性的图像。
28 | */
29 | private String quality;
30 |
31 | /**
32 | * 返回生成的图像的格式:url、b64_json
33 | */
34 | @JsonProperty("response_format")
35 | private String responseFormat;
36 |
37 | /**
38 | * 代表最终用户的唯一标识符
39 | */
40 | private String user;
41 |
42 | /**
43 | * 图片生成模型
44 | */
45 | @Getter
46 | @AllArgsConstructor
47 | public enum Model {
48 | DALL_E_2("dall-e-2"),
49 | DALL_E_3("dall-e-3"),
50 | ;
51 | private final String name;
52 | }
53 |
54 | /**
55 | * 生成图片质量
56 | */
57 | @Getter
58 | @AllArgsConstructor
59 | public enum Quality {
60 | STANDARD("standard"),
61 | HD("hd"),
62 | ;
63 | private final String quality;
64 | }
65 |
66 | /**
67 | * 生成图片风格
68 | */
69 | @Getter
70 | @AllArgsConstructor
71 | public enum Style {
72 | VIVID("vivid"),
73 | NATURAL("natural"),
74 | ;
75 | private final String style;
76 | }
77 |
78 | @Getter
79 | @AllArgsConstructor
80 | public enum Format {
81 | URL("url"),
82 | B64JSON("b64_json"),
83 | ;
84 | private final String format;
85 | }
86 | }
87 |
--------------------------------------------------------------------------------
/smartFuse-openai/src/main/java/com/ai/openai/parameter/input/OpenaiModerationParameter.java:
--------------------------------------------------------------------------------
1 | package com.ai.openai.parameter.input;
2 |
3 | import com.ai.openai.endPoint.moderations.req.ModerationRequest;
4 | import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
5 | import com.fasterxml.jackson.annotation.JsonInclude;
6 | import lombok.*;
7 |
8 | import java.io.Serializable;
9 |
10 | @Data
11 | @Builder
12 | @NoArgsConstructor
13 | @AllArgsConstructor
14 | @JsonIgnoreProperties(ignoreUnknown = true)
15 | @JsonInclude(JsonInclude.Include.NON_NULL)
16 | public class OpenaiModerationParameter implements Serializable {
17 |
18 | @Builder.Default
19 | private String model = ModerationRequest.Model.TEXT_MODERATION_LATEST.getName();
20 |
21 | @Getter
22 | @AllArgsConstructor
23 | public enum Model {
24 | TEXT_MODERATION_STABLE("text-moderation-stable"),
25 | TEXT_MODERATION_LATEST("text-moderation-latest"),
26 | ;
27 |
28 | private final String name;
29 | }
30 |
31 | }
32 |
--------------------------------------------------------------------------------
/smartFuse-openai/src/test/java/com/ai/openai/chain/ConversationalChainTest.java:
--------------------------------------------------------------------------------
1 | package com.ai.openai.chain;
2 |
3 | import com.ai.core.strategy.impl.FirstKeyStrategy;
4 | import com.ai.domain.chain.impl.ConversationalChain;
5 | import com.ai.domain.memory.chat.impl.SimpleChatHistoryRecorder;
6 | import com.ai.openai.achieve.Configuration;
7 | import com.ai.openai.client.OpenAiClient;
8 | import com.ai.openai.model.OpenaiChatModel;
9 | import org.junit.Before;
10 | import org.junit.Test;
11 |
12 | import java.net.InetSocketAddress;
13 | import java.net.Proxy;
14 | import java.util.Arrays;
15 |
16 | /**
17 | * 测试链路功能
18 | **/
19 | public class ConversationalChainTest {
20 |
21 | private ConversationalChain conversationalChain;
22 |
23 | @Before
24 | public void test_create_conversational_chain() {
25 | // 设置配置信息
26 | Configuration configuration = new Configuration();
27 | configuration.setApiHost("https://api.openai.com");
28 | configuration.setKeyList(Arrays.asList("************************"));
29 | configuration.setKeyStrategy(new FirstKeyStrategy());
30 | configuration.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890)));
31 | OpenAiClient.SetConfiguration(configuration);
32 | // 创建一个记录器,记录器是不能重用的,即不能多个chain使用同一个记录器,否则就相当于多个会话公用同一个历史聊天记录。
33 | // 但是记录器对应的存储器,及记录器当中的ChatMemoryStore是可以重用的,及存在多个记录器使用同一个存储器。
34 | // 可以在创建时指定记录器,也可以直接创建使用默认的记录器,默认存储30条消息。
35 | this.conversationalChain = ConversationalChain.builder()
36 | .chatModel(new OpenaiChatModel())
37 | .historyRecorder(SimpleChatHistoryRecorder.builder().build())
38 | .build();
39 | }
40 |
41 | @Test
42 | public void test_conversational_chain_run() {
43 | String res1 = conversationalChain.run("你好,请记住我的名字叫做小明");
44 | System.out.println(res1);// 你好,小明!很高兴认识你。
45 | String res2 = conversationalChain.run("我的名字是什么?");
46 | System.out.println(res2);// 你的名字是小明。
47 | }
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/smartFuse-openai/src/test/java/com/ai/openai/chain/ConversationalRetrievalChainTest.java:
--------------------------------------------------------------------------------
1 | package com.ai.openai.chain;
2 |
3 |
4 | import com.ai.core.strategy.impl.FirstKeyStrategy;
5 | import com.ai.domain.chain.impl.ConversationalRetrievalChain;
6 | import com.ai.domain.document.Document;
7 | import com.ai.domain.document.FileSystemDocumentLoader;
8 | import com.ai.domain.memory.chat.impl.SimpleChatHistoryRecorder;
9 | import com.ai.domain.memory.embedding.impl.SimpleEmbeddingStoreIngestor;
10 | import com.ai.domain.memory.embedding.impl.SimpleEmbeddingStoreRetriever;
11 | import com.ai.openai.achieve.Configuration;
12 | import com.ai.openai.client.OpenAiClient;
13 | import com.ai.openai.model.OpenaiChatModel;
14 | import com.ai.openai.model.OpenaiEmbeddingModel;
15 | import org.junit.Before;
16 | import org.junit.Test;
17 |
18 | import java.io.File;
19 | import java.net.InetSocketAddress;
20 | import java.net.Proxy;
21 | import java.nio.file.Path;
22 | import java.nio.file.Paths;
23 | import java.util.ArrayList;
24 | import java.util.Arrays;
25 | import java.util.List;
26 |
27 | public class ConversationalRetrievalChainTest {
28 |
29 | private ConversationalRetrievalChain conversationalRetrievalChain;
30 |
31 | public static Path toPath(String fileName) {
32 | File file = new File(fileName);
33 | if (file.exists()) {
34 | try {
35 | return Paths.get(file.toURI());
36 | } catch (Exception e) {
37 | e.printStackTrace();
38 | }
39 | }
40 | return null;
41 | }
42 |
43 | @Before
44 | public void before() {
45 | // 设置配置信息
46 | Configuration configuration = new Configuration();
47 | configuration.setApiHost("https://api.openai.com");
48 | configuration.setKeyList(Arrays.asList("************************"));
49 | configuration.setKeyStrategy(new FirstKeyStrategy());
50 | configuration.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890)));
51 | OpenAiClient.SetConfiguration(configuration);
52 | // 测试文件路径
53 | String[] filePath = {"D:\\chatGPT-api\\AI-SmartFuse-Framework\\doc\\test\\document\\ConversationalRetrievalChainTest-中文.txt"};
54 | // "D:\\chatGPT-api\\AI-SmartFuse-Framework\\doc\\test\\document\\ConversationalRetrievalChainTest-英文.txt"};
55 | // 创建嵌入数据导入器,这里可以设置你指定的存储器,也可以直接使用其中默认的存储器。
56 | SimpleEmbeddingStoreIngestor ingestor = SimpleEmbeddingStoreIngestor.builder().embeddingModel(new OpenaiEmbeddingModel()).build();
57 | List documents = new ArrayList<>();
58 | // 导入数据并放入List当中
59 | for (String file : filePath) {
60 | documents.add(FileSystemDocumentLoader.loadDocument(toPath(file)));
61 | }
62 | // 将数据导入到存储器当中
63 | ingestor.ingest(documents);
64 | // 获取存储器,并设置其对应的检索器,向检索器当中设置检索器检索的嵌入存储器。
65 | this.conversationalRetrievalChain = ConversationalRetrievalChain.builder()
66 | .chatModel(new OpenaiChatModel())
67 | .embeddingModel(new OpenaiEmbeddingModel())
68 | .historyRecorder(SimpleChatHistoryRecorder.builder().build())
69 | .retriever(SimpleEmbeddingStoreRetriever.builder().embeddingMemoryStore(ingestor.getStore()).build())
70 | .build();
71 | }
72 |
73 | @Test
74 | public void test_embedding_data_retriever_with_en() {
75 | String question = "What kind of person is Little Red Riding Hood?";
76 | String res = conversationalRetrievalChain.run(question);
77 | System.out.println(res);
78 | }
79 |
80 | @Test
81 | public void test_embedding_data_retriever_with_ch() {
82 | String question = "小红帽要去干什么?";
83 | String res = conversationalRetrievalChain.run(question);
84 | System.out.println(res);
85 | }
86 |
87 |
88 | }
89 |
--------------------------------------------------------------------------------
/smartFuse-openai/src/test/java/com/ai/openai/model/ModelTest.java:
--------------------------------------------------------------------------------
1 | package com.ai.openai.model;
2 |
3 |
4 | import com.ai.common.resp.AiResponse;
5 | import com.ai.core.strategy.impl.FirstKeyStrategy;
6 | import com.ai.domain.data.moderation.Moderation;
7 | import com.ai.openai.achieve.Configuration;
8 | import com.ai.openai.client.OpenAiClient;
9 | import org.junit.Before;
10 | import org.junit.Test;
11 |
12 | import java.net.InetSocketAddress;
13 | import java.net.Proxy;
14 | import java.util.ArrayList;
15 | import java.util.Arrays;
16 | import java.util.List;
17 |
18 | public class ModelTest {
19 |
20 | @Before
21 | public void test_model_before() {
22 | // 设置配置信息
23 | Configuration configuration = new Configuration();
24 | configuration.setApiHost("https://api.openai.com");
25 | configuration.setKeyList(Arrays.asList("************************"));
26 | configuration.setKeyStrategy(new FirstKeyStrategy());
27 | configuration.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 7890)));
28 | OpenAiClient.SetConfiguration(configuration);
29 | }
30 |
31 | @Test
32 | public void test_moderation_model() {
33 | OpenaiModerationModel openaiModerationModel = new OpenaiModerationModel();
34 | ArrayList strings = new ArrayList<>();
35 | strings.add("你好");
36 | strings.add("我要杀了你");
37 | AiResponse