Files
AIRouter/examples/demo_usage.go
T
freedakgmail 5bcfbdb88d
CI / lint (push) Has been cancelled
CI / test (push) Has been cancelled
CI / build (push) Has been cancelled
CI / security-scan (push) Has been cancelled
docs: 添加使用示范和启动指南
- examples/demo_usage.py: Python 使用示范(openai SDK + requests)
- examples/demo_usage.go: Go 使用示范(net/http)
- run.md: 完整启动指南(前置条件、3种启动方式、API Key 配置、验证命令)
2026-08-03 07:57:31 +08:00

142 lines
3.8 KiB
Go

// Edge AI Gateway 使用示范 (Go 版本)
//
// 演示 Go 应用如何调用 Edge AI Gateway 的 OpenAI 兼容 API。
//
// 运行:go run examples/demo_usage.go
package main
import (
"bufio"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
)
const (
BaseURL = "http://localhost:8080"
APIKey = "test-key"
)
func main() {
// === 1. 非流式对话 ===
fmt.Println("=== 非流式对话 ===")
resp := doPost(BaseURL+"/v1/chat/completions", map[string]any{
"model": "general-chat",
"messages": []map[string]string{{"role": "user", "content": "你好,介绍一下你自己"}},
"stream": false,
})
fmt.Printf("回复: %s\n", resp["choices"].([]any)[0].(map[string]any)["message"].(map[string]any)["content"])
fmt.Printf("模型: %s\n", resp["actual_model"])
fmt.Println()
// === 2. 流式对话 (SSE) ===
fmt.Println("=== 流式对话 ===")
doStream(BaseURL+"/v1/chat/completions", map[string]any{
"model": "fast-chat",
"messages": []map[string]string{{"role": "user", "content": "写一个Go hello world"}},
"stream": true,
})
fmt.Println()
// === 3. 查看模型列表 ===
fmt.Println("=== 可用模型 ===")
resp2 := doGet(BaseURL + "/v1/models")
for _, m := range resp2["data"].([]any) {
fmt.Printf(" - %s\n", m.(map[string]any)["id"])
}
fmt.Println()
// === 4. 会话管理 ===
fmt.Println("=== 会话管理 ===")
sess := doPost(BaseURL+"/v1/sessions", map[string]any{
"application_id": "demo-app",
"user_id": "user1",
})
sessionID := sess["session_id"].(string)
fmt.Printf("创建会话: %s\n\n", sessionID)
// === 5. 错误处理 ===
fmt.Println("=== 错误处理 ===")
// 无认证
resp3, _ := http.Get(BaseURL + "/v1/models")
fmt.Printf("无认证: HTTP %d\n", resp3.StatusCode)
resp3.Body.Close()
// 缺少 model
resp4 := doPost(BaseURL+"/v1/chat/completions", map[string]any{
"messages": []map[string]string{{"role": "user", "content": "hi"}},
})
if err, ok := resp4["error"]; ok {
fmt.Printf("缺少model: %s\n", err.(map[string]any)["code"])
}
fmt.Println("\n✅ 示范完成!")
}
func doPost(url string, body map[string]any) map[string]any {
b, _ := json.Marshal(body)
req, _ := http.NewRequest("POST", url, strings.NewReader(string(b)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+APIKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("请求失败: %v\n", err)
return nil
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
var result map[string]any
json.Unmarshal(data, &result)
return result
}
func doGet(url string) map[string]any {
req, _ := http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", "Bearer "+APIKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("请求失败: %v\n", err)
return nil
}
defer resp.Body.Close()
data, _ := io.ReadAll(resp.Body)
var result map[string]any
json.Unmarshal(data, &result)
return result
}
func doStream(url string, body map[string]any) {
b, _ := json.Marshal(body)
req, _ := http.NewRequest("POST", url, strings.NewReader(string(b)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+APIKey)
resp, err := http.DefaultClient.Do(req)
if err != nil {
fmt.Printf("请求失败: %v\n", err)
return
}
defer resp.Body.Close()
scanner := bufio.NewScanner(resp.Body)
for scanner.Scan() {
line := scanner.Text()
if !strings.HasPrefix(line, "data: ") {
continue
}
data := strings.TrimPrefix(line, "data: ")
if data == "[DONE]" {
return
}
var chunk map[string]any
json.Unmarshal([]byte(data), &chunk)
choices := chunk["choices"].([]any)
delta := choices[0].(map[string]any)["delta"].(map[string]any)
if content, ok := delta["content"]; ok {
fmt.Print(content.(string))
}
}
if err := scanner.Err(); err != nil {
fmt.Printf("\nstream error: %v\n", err)
}
}