Moonshort API Examples
本页面提供Agentsflare Moonshort API的使用示例,帮助您快速集成和使用我们的AI服务。
基础配置
在开始使用API之前,请确保您已经获取了API Key。如果还没有,请参考创建API Key。
基础信息
- API Base URL:
https://api.agentsflare.com/v1/chat/completions - 认证方式: Bearer Token
- 内容类型:
application/json
请求示例
bash
curl -X POST "https://api.agentsflare.com/v1/chat/completions" \
-H "Authorization: Bearer YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"model": "kimi-k2-turbo-preview",
"messages": [
{"role": "system", "content": "You are Kimi, an AI assistant provided by Moonshot AI. You are proficient in Chinese and English conversations. You provide users with safe, helpful, and accurate answers. You will reject any questions involving terrorism, racism, or explicit content. Moonshot AI is a proper noun and should not be translated."},
{"role": "user", "content": "Hello, my name is Li Lei. What is 1+1?"}
],
"temperature": 0.6
}'python
from openai import OpenAI
url = "https://api.agentsflare.com/v1"
client = OpenAI(
base_url=url,
api_key="YOUR_API_KEY"
)
completion = client.chat.completions.create(
model = "kimi-k2-turbo-preview",
messages = [
{"role": "system", "content": "You are Kimi, an AI assistant provided by Moonshot AI. You are proficient in Chinese and English conversations. You provide users with safe, helpful, and accurate answers. You will reject any questions involving terrorism, racism, or explicit content. Moonshot AI is a proper noun and should not be translated."},
{"role": "user", "content": "Hello, my name is Li Lei. What is 1+1?"}
],
temperature = 0.6,
)
print(completion.choices[0].message.content)javascript
const { OpenAI } = require("openai");
const openai = new OpenAI({
apiKey: "YOUR_API_KEY",
baseURL: "https://api.agentsflare.com/v1", // 自定义API地址
});
async function main() {
const completion = await openai.chat.completions.create({
model: "kimi-k2-turbo-preview",
messages: [
{
role: "system",
content: "You are Kimi, an AI assistant provided by Moonshot AI. You are proficient in Chinese and English conversations. You provide users with safe, helpful, and accurate answers. You will reject any questions involving terrorism, racism, or explicit content. Moonshot AI is a proper noun and should not be translated."
},
{
role: "user",
content: "Hello, my name is Li Lei. What is 1+1?"
}
],
temperature: 0.6,
});
console.log(completion.choices[0].message.content);
}
main();java
import com.openai.client.OpenAIClient;
import com.openai.client.okhttp.OpenAIOkHttpClient;
import com.openai.models.chat.completions.ChatCompletionCreateParams;
import com.openai.models.chat.completions.ChatCompletion;
public class OpenAIChat {
public static void main(String[] args) {
String apiKey = "YOUR_API_KEY";
String baseUrl = "https://api.agentsflare.com/v1"; // 你的自定义API地址
OpenAiService service = new OpenAiService(apiKey);
service.setOpenAiApiBase(baseUrl);
ChatMessage systemMessage = new ChatMessage("system",
"You are Kimi, an AI assistant provided by Moonshot AI. You are proficient in Chinese and English conversations. You provide users with safe, helpful, and accurate answers. You will reject any questions involving terrorism, racism, or explicit content. Moonshot AI is a proper noun and should not be translated.");
ChatMessage userMessage = new ChatMessage("user", "Hello, my name is Li Lei. What is 1+1?");
ChatCompletionRequest request = ChatCompletionRequest.builder()
.model("kimi-k2-turbo-preview")
.messages(Arrays.asList(systemMessage, userMessage))
.temperature(0.6)
.build();
ChatCompletionResult result = service.createChatCompletion(request);
String content = result.getChoices().get(0).getMessage().getContent();
System.out.println(content);
}
}go
package main
import (
"context"
"fmt"
"github.com/sashabaranov/go-openai"
)
func main() {
apiKey := "YOUR_API_KEY"
baseURL := "https://api.agentsflare.com/v1"
config := openai.DefaultConfig(apiKey)
config.BaseURL = baseURL
client := openai.NewClientWithConfig(config)
resp, err := client.CreateChatCompletion(
context.Background(),
openai.ChatCompletionRequest{
Model: "kimi-k2-turbo-preview",
Messages: []openai.ChatCompletionMessage{
{
Role: openai.ChatMessageRoleSystem,
Content: "You are Kimi, an AI assistant provided by Moonshot AI. You are proficient in Chinese and English conversations. You provide users with safe, helpful, and accurate answers. You will reject any questions involving terrorism, racism, or explicit content. Moonshot AI is a proper noun and should not be translated.",
},
{
Role: openai.ChatMessageRoleUser,
Content: "Hello, my name is Li Lei. What is 1+1?",
},
},
Temperature: 0.6,
},
)
if err != nil {
panic(err)
}
fmt.Println(resp.Choices[0].Message.Content)
}javascript
const { OpenAI } = require("openai");
const client = new OpenAI({
apiKey: process.env.AGENTSFLARE_API_KEY,
baseURL: "https://api.agentsflare.com/v1"
});
async function main() {
const completion = await client.chat.completions.create({
model: "kimi-k2-turbo-preview",
messages: [
{role: "system", content: "You are Kimi, an AI assistant provided by Moonshot AI. You are proficient in Chinese and English conversations. You provide users with safe, helpful, and accurate answers. You will reject any questions involving terrorism, racism, or explicit content. Moonshot AI is a proper noun and should not be translated."},
{role: "user", content: "Hello, my name is Li Lei. What is 1+1?"}
],
temperature: 0.6
});
console.log(completion.choices[0].message.content);
}
main();响应示例
{
"id": "chatcmpl-6964a0f86d1b90a78b320e54",
"object": "chat.completion",
"created": 1768202488,
"model": "kimi-k2-0905-preview",
"choices": [
{
"index": 0,
"message": {
"role": "assistant",
"content": "Hello, Li Lei! \n1 + 1 = 2"
},
"finish_reason": "stop"
}
],
"usage": {
"prompt_tokens": 89,
"completion_tokens": 15,
"total_tokens": 104
}
}