地址
首先进入官方火山方舟管理控制台,账号登录-火山引擎 (volcengine.com)。
选择所需模型,开通api
首先开通api
选择一个模型开通,有一定免费额度
代码调用api实现大模型调用
进入对应模型,找到模型的ID
依赖准备,这边使用java版本的
<dependency>
<groupId>com.volcengine</groupId>
<artifactId>volcengine-java-sdk-ark-runtime</artifactId>
<version>LATEST</version>
</dependency>
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>LATEST</version>
</dependency>
具体所需哪些依赖详见帮助文档
使用帮助文档提供示例,填入所需参数
注意填入需要的模型和api
package simple;
import com.volcengine.ark.runtime.model.completion.chat.ChatCompletionRequest;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessage;
import com.volcengine.ark.runtime.model.completion.chat.ChatMessageRole;
import com.volcengine.ark.runtime.service.ArkService;
import java.util.ArrayList;
import java.util.List;
public class ChatCompletionsExample {
/**
* Authentication
* 1.If you authorize your endpoint using an API key, you can set your api key to environment variable "ARK_API_KEY"
* String apiKey = System.getenv("ARK_API_KEY");
* ArkService service = new ArkService(apiKey);
* Note: If you use an API key, this API key will not be refreshed.
* To prevent the API from expiring and failing after some time, choose an API key with no expiration date.
*
* 2.If you authorize your endpoint with Volcengine Identity and Access Management(IAM), set your api key to environment variable "VOLC_ACCESSKEY", "VOLC_SECRETKEY"
* String ak = System.getenv("VOLC_ACCESSKEY");
* String sk = System.getenv("VOLC_SECRETKEY");
* ArkService service = new ArkService(ak, sk);
* To get your ak&sk, please refer to this document(https://www.volcengine.com/docs/6291/65568)
* For more information,please check this document(https://www.volcengine.com/docs/82379/1263279)
*/
public static void main(String[] args) {
String apiKey = "de8afed8-033c";
ArkService service = new ArkService(apiKey);
System.out.println("\n----- standard request -----");
final List<ChatMessage> messages = new ArrayList<>();
final ChatMessage systemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage userMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的牛马植物有哪些?").build();
messages.add(systemMessage);
messages.add(userMessage);
ChatCompletionRequest chatCompletionRequest = ChatCompletionRequest.builder()
.model("ep-20240702071522-bdf4h")
.messages(messages)
.build();
service.createChatCompletion(chatCompletionRequest).getChoices().forEach(choice -> System.out.println(choice.getMessage().getContent()));
System.out.println("\n----- streaming request -----");
final List<ChatMessage> streamMessages = new ArrayList<>();
final ChatMessage streamSystemMessage = ChatMessage.builder().role(ChatMessageRole.SYSTEM).content("你是豆包,是由字节跳动开发的 AI 人工智能助手").build();
final ChatMessage streamUserMessage = ChatMessage.builder().role(ChatMessageRole.USER).content("常见的十字花科植物有哪些?").build();
streamMessages.add(streamSystemMessage);
streamMessages.add(streamUserMessage);
ChatCompletionRequest streamChatCompletionRequest = ChatCompletionRequest.builder()
.model("ep-20240702071522-bdf4h")
.messages(streamMessages)
.build();
service.streamChatCompletion(streamChatCompletionRequest)
.doOnError(Throwable::printStackTrace)
.blockingForEach(
choice -> {
if (choice.getChoices().size() > 0) {
System.out.print(choice.getChoices().get(0).getMessage().getContent());
}
}
);
// shutdown service
service.shutdownExecutor();
}