Commit 5bdc2db0c38d1641379d6038378655b3a7f04d14
1 parent
83bd96d6
【功能新增】AI:集成 Azure 的 OpenAI 模型
Showing
2 changed files
with
71 additions
and
2 deletions
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/AzureOpenAIChatModelTests.java
0 → 100644
| 1 | +package cn.iocoder.yudao.framework.ai.chat; | ||
| 2 | + | ||
| 3 | +import com.azure.ai.openai.OpenAIClient; | ||
| 4 | +import com.azure.ai.openai.OpenAIClientBuilder; | ||
| 5 | +import com.azure.core.credential.AzureKeyCredential; | ||
| 6 | +import com.azure.core.util.ClientOptions; | ||
| 7 | +import org.junit.jupiter.api.Disabled; | ||
| 8 | +import org.junit.jupiter.api.Test; | ||
| 9 | +import org.springframework.ai.azure.openai.AzureOpenAiChatModel; | ||
| 10 | +import org.springframework.ai.azure.openai.AzureOpenAiChatOptions; | ||
| 11 | +import org.springframework.ai.chat.messages.Message; | ||
| 12 | +import org.springframework.ai.chat.messages.SystemMessage; | ||
| 13 | +import org.springframework.ai.chat.messages.UserMessage; | ||
| 14 | +import org.springframework.ai.chat.model.ChatResponse; | ||
| 15 | +import org.springframework.ai.chat.prompt.Prompt; | ||
| 16 | +import reactor.core.publisher.Flux; | ||
| 17 | + | ||
| 18 | +import java.util.ArrayList; | ||
| 19 | +import java.util.List; | ||
| 20 | + | ||
| 21 | +import static org.springframework.ai.autoconfigure.azure.openai.AzureOpenAiChatProperties.DEFAULT_DEPLOYMENT_NAME; | ||
| 22 | + | ||
| 23 | +/** | ||
| 24 | + * {@link AzureOpenAiChatModel} 集成测试 | ||
| 25 | + * | ||
| 26 | + * @author 芋道源码 | ||
| 27 | + */ | ||
| 28 | +public class AzureOpenAIChatModelTests { | ||
| 29 | + | ||
| 30 | + private final OpenAIClient openAiApi = (new OpenAIClientBuilder()) | ||
| 31 | + .endpoint("https://eastusprejade.openai.azure.com") | ||
| 32 | + .credential(new AzureKeyCredential("xxx")) | ||
| 33 | + .clientOptions((new ClientOptions()).setApplicationId("spring-ai")) | ||
| 34 | + .buildClient(); | ||
| 35 | + private final AzureOpenAiChatModel chatModel = new AzureOpenAiChatModel(openAiApi, | ||
| 36 | + AzureOpenAiChatOptions.builder().withDeploymentName(DEFAULT_DEPLOYMENT_NAME).build()); | ||
| 37 | + | ||
| 38 | + @Test | ||
| 39 | + @Disabled | ||
| 40 | + public void testCall() { | ||
| 41 | + // 准备参数 | ||
| 42 | + List<Message> messages = new ArrayList<>(); | ||
| 43 | + messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。")); | ||
| 44 | + messages.add(new UserMessage("1 + 1 = ?")); | ||
| 45 | + | ||
| 46 | + // 调用 | ||
| 47 | + ChatResponse response = chatModel.call(new Prompt(messages)); | ||
| 48 | + // 打印结果 | ||
| 49 | + System.out.println(response); | ||
| 50 | + System.out.println(response.getResult().getOutput()); | ||
| 51 | + } | ||
| 52 | + | ||
| 53 | + @Test | ||
| 54 | + @Disabled | ||
| 55 | + public void testStream() { | ||
| 56 | + // 准备参数 | ||
| 57 | + List<Message> messages = new ArrayList<>(); | ||
| 58 | + messages.add(new SystemMessage("你是一个优质的文言文作者,用文言文描述着各城市的人文风景。")); | ||
| 59 | + messages.add(new UserMessage("1 + 1 = ?")); | ||
| 60 | + | ||
| 61 | + // 调用 | ||
| 62 | + Flux<ChatResponse> flux = chatModel.stream(new Prompt(messages)); | ||
| 63 | + // 打印结果 | ||
| 64 | + flux.doOnNext(response -> { | ||
| 65 | +// System.out.println(response); | ||
| 66 | + System.out.println(response.getResult().getOutput()); | ||
| 67 | + }).then().block(); | ||
| 68 | + } | ||
| 69 | + | ||
| 70 | +} |
yudao-module-ai/yudao-spring-boot-starter-ai/src/test/java/cn/iocoder/yudao/framework/ai/chat/OpenAIChatModelTests.java
| 1 | package cn.iocoder.yudao.framework.ai.chat; | 1 | package cn.iocoder.yudao.framework.ai.chat; |
| 2 | 2 | ||
| 3 | -import cn.iocoder.yudao.framework.ai.core.model.xinghuo.XingHuoChatModel; | ||
| 4 | import org.junit.jupiter.api.Disabled; | 3 | import org.junit.jupiter.api.Disabled; |
| 5 | import org.junit.jupiter.api.Test; | 4 | import org.junit.jupiter.api.Test; |
| 6 | import org.springframework.ai.chat.messages.Message; | 5 | import org.springframework.ai.chat.messages.Message; |
| @@ -17,7 +16,7 @@ import java.util.ArrayList; | @@ -17,7 +16,7 @@ import java.util.ArrayList; | ||
| 17 | import java.util.List; | 16 | import java.util.List; |
| 18 | 17 | ||
| 19 | /** | 18 | /** |
| 20 | - * {@link XingHuoChatModel} 集成测试 | 19 | + * {@link OpenAiChatModel} 集成测试 |
| 21 | * | 20 | * |
| 22 | * @author 芋道源码 | 21 | * @author 芋道源码 |
| 23 | */ | 22 | */ |