feat: initialize Eino examples and ADK applications
Include chat, weather, image, travel, streaming ASR and text examples, shared model initialization, documentation, and existing tests. Co-authored-by: TRAE CLI <traecli@bytedance.com>
This commit is contained in:
commit
81deca8c8e
75 files changed
+7048
No files matched your search
@@ -0,0 +1,36 @@
|
||||
package model
|
||||
|
||||
// ASREvent 只包含实时转写接口使用的事件字段。
|
||||
type ASREvent struct {
|
||||
Type string `json:"type"`
|
||||
ItemID string `json:"item_id,omitempty"`
|
||||
Delta string `json:"delta,omitempty"`
|
||||
Transcript string `json:"transcript,omitempty"`
|
||||
Audio string `json:"audio,omitempty"`
|
||||
Error *struct {
|
||||
Message string `json:"message"`
|
||||
} `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
// ASRSessionUpdate 配置中文、单声道 PCM 输入和手动提交。
|
||||
type ASRSessionUpdate struct {
|
||||
Type string `json:"type"`
|
||||
Session ASRSession `json:"session"`
|
||||
}
|
||||
|
||||
type ASRSession struct {
|
||||
Type string `json:"type"`
|
||||
Audio struct {
|
||||
Input struct {
|
||||
Format struct {
|
||||
Type string `json:"type"`
|
||||
Rate int `json:"rate"`
|
||||
} `json:"format"`
|
||||
Transcription struct {
|
||||
Model string `json:"model"`
|
||||
Language string `json:"language"`
|
||||
} `json:"transcription"`
|
||||
TurnDetection any `json:"turn_detection"`
|
||||
} `json:"input"`
|
||||
} `json:"audio"`
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package model
|
||||
|
||||
// ChatCompletionRequest 表示对话接口的请求参数。
|
||||
type ChatCompletionRequest struct {
|
||||
Model string `json:"model"`
|
||||
Messages []ChatMessage `json:"messages"`
|
||||
Stream bool `json:"stream,omitempty"`
|
||||
Tools []ToolDefinition `json:"tools,omitempty"`
|
||||
}
|
||||
|
||||
// ChatCompletionResponse 表示对话接口的响应。
|
||||
type ChatCompletionResponse struct {
|
||||
Choices []ChatCompletionChoice `json:"choices"`
|
||||
}
|
||||
|
||||
// ChatCompletionChoice 表示模型返回的一个候选回答。
|
||||
type ChatCompletionChoice struct {
|
||||
Message ChatMessage `json:"message"`
|
||||
Delta ChatCompletionDelta `json:"delta"`
|
||||
FinishReason string `json:"finish_reason"`
|
||||
}
|
||||
|
||||
// ChatCompletionDelta 表示流式响应中的增量内容。
|
||||
type ChatCompletionDelta struct {
|
||||
Content string `json:"content"`
|
||||
ToolCalls []ToolCall `json:"tool_calls"`
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
package model
|
||||
|
||||
// ImageGenerationRequest 是 OpenAI 兼容图片接口的请求。
|
||||
type ImageGenerationRequest struct {
|
||||
Model string `json:"model"`
|
||||
Prompt string `json:"prompt"`
|
||||
N int `json:"n"`
|
||||
}
|
||||
|
||||
type ImageGenerationResponse struct {
|
||||
Data []ImageGenerationData `json:"data"`
|
||||
}
|
||||
|
||||
type ImageGenerationData struct {
|
||||
Base64 string `json:"b64_json"`
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// ImageArguments 是生图工具接收的创作提示词。
|
||||
type ImageArguments struct {
|
||||
Prompt string `json:"prompt"`
|
||||
}
|
||||
|
||||
// ImageResult 返回已经保存到本地的图片路径。
|
||||
type ImageResult struct {
|
||||
Path string `json:"path"`
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package model
|
||||
|
||||
import "strings"
|
||||
|
||||
type ChatInputType string
|
||||
|
||||
const (
|
||||
ChatInputTypeTxt ChatInputType = "text"
|
||||
ChatInputTypeImg ChatInputType = "image"
|
||||
)
|
||||
|
||||
type ChatInput struct {
|
||||
Type ChatInputType
|
||||
// 文字为原文,图片为图片地址或 base64 data URL。
|
||||
Data string
|
||||
}
|
||||
|
||||
type ChatInputs []*ChatInput
|
||||
|
||||
// Text 返回用于保存历史的文字,图片用占位文字表示。
|
||||
func (i ChatInputs) Text() string {
|
||||
var texts []string
|
||||
for _, input := range i {
|
||||
if input == nil {
|
||||
continue
|
||||
}
|
||||
switch input.Type {
|
||||
case ChatInputTypeTxt:
|
||||
texts = append(texts, input.Data)
|
||||
case ChatInputTypeImg:
|
||||
texts = append(texts, "[图片]")
|
||||
}
|
||||
}
|
||||
return strings.Join(texts, "\n")
|
||||
}
|
||||
|
||||
func (i ChatInputs) Format() *ChatMessage {
|
||||
if len(i) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
cs := make([]any, 0)
|
||||
|
||||
for _, item := range i {
|
||||
if item == nil || item.Data == "" {
|
||||
continue
|
||||
}
|
||||
switch item.Type {
|
||||
case ChatInputTypeTxt:
|
||||
cs = append(cs, map[string]string{
|
||||
"type": "text",
|
||||
"text": item.Data,
|
||||
})
|
||||
case ChatInputTypeImg:
|
||||
cs = append(cs, map[string]any{
|
||||
"type": "image_url",
|
||||
"image_url": map[string]string{"url": item.Data},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if len(cs) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
return &ChatMessage{
|
||||
Role: "user",
|
||||
Content: cs,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package model
|
||||
|
||||
// ChatMessage 表示用户、模型或工具的一条消息。
|
||||
type ChatMessage struct {
|
||||
Role string `json:"role"`
|
||||
Content any `json:"content"`
|
||||
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
|
||||
ToolCallID string `json:"tool_call_id,omitempty"`
|
||||
}
|
||||
|
||||
// ChatContentPart 表示用户消息中的一段文字或图片。
|
||||
type ChatContentPart struct {
|
||||
Type string `json:"type"`
|
||||
Text string `json:"text,omitempty"`
|
||||
ImageURL *ChatImageURL `json:"image_url,omitempty"`
|
||||
}
|
||||
|
||||
// ChatImageURL 保存图片地址,也支持 base64 data URL。
|
||||
type ChatImageURL struct {
|
||||
URL string `json:"url"`
|
||||
}
|
||||
|
||||
// ToolCall 表示模型请求执行的工具调用。
|
||||
type ToolCall struct {
|
||||
Index *int `json:"index,omitempty"`
|
||||
ID string `json:"id"`
|
||||
Type string `json:"type"`
|
||||
Function ToolFunction `json:"function"`
|
||||
}
|
||||
|
||||
// ToolFunction 表示工具调用的函数名称和参数。
|
||||
type ToolFunction struct {
|
||||
Name string `json:"name"`
|
||||
Arguments string `json:"arguments"`
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package model
|
||||
|
||||
// ToolDefinition 描述可供模型调用的工具。
|
||||
type ToolDefinition struct {
|
||||
Type string `json:"type"`
|
||||
Function ToolFunctionDefinition `json:"function"`
|
||||
}
|
||||
|
||||
// ToolFunctionDefinition 包含工具名称、用途和参数的 JSON Schema。
|
||||
type ToolFunctionDefinition struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
Parameters map[string]any `json:"parameters"`
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package model
|
||||
|
||||
// TravelRequest 是主助手提取的出行需求,日期使用北京时间。
|
||||
type TravelRequest struct {
|
||||
Location string `json:"location"`
|
||||
StartDate string `json:"start_date"`
|
||||
EndDate string `json:"end_date,omitempty"`
|
||||
Requirements string `json:"requirements,omitempty"`
|
||||
}
|
||||
|
||||
// TravelGuide 在 Graph 节点之间传递天气、行程和图片结果。
|
||||
type TravelGuide struct {
|
||||
Request TravelRequest `json:"request"`
|
||||
Weather WeatherForecast `json:"weather"`
|
||||
Plan string `json:"plan,omitempty"`
|
||||
ImagePrompt string `json:"image_prompt,omitempty"`
|
||||
Path string `json:"path,omitempty"`
|
||||
Problem string `json:"problem,omitempty"`
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package model
|
||||
|
||||
// WeatherArguments 指定地点,以及可选的预报日期范围。
|
||||
type WeatherArguments struct {
|
||||
Location string `json:"location"`
|
||||
StartDate string `json:"start_date,omitempty"`
|
||||
EndDate string `json:"end_date,omitempty"`
|
||||
}
|
||||
|
||||
// WeatherLocations 是 Open-Meteo 的城市搜索结果,使用首个匹配项。
|
||||
type WeatherLocations struct {
|
||||
Results []struct {
|
||||
Name string `json:"name"`
|
||||
Admin1 string `json:"admin1"`
|
||||
Country string `json:"country"`
|
||||
Latitude float64 `json:"latitude"`
|
||||
Longitude float64 `json:"longitude"`
|
||||
} `json:"results"`
|
||||
}
|
||||
|
||||
// WeatherDailyResponse 只保留本示例使用的逐日预报字段。
|
||||
type WeatherDailyResponse struct {
|
||||
Daily struct {
|
||||
Dates []string `json:"time"`
|
||||
Codes []*int `json:"weather_code"`
|
||||
MaxTemps []*float64 `json:"temperature_2m_max"`
|
||||
MinTemps []*float64 `json:"temperature_2m_min"`
|
||||
RainChances []*float64 `json:"precipitation_probability_max"`
|
||||
} `json:"daily"`
|
||||
}
|
||||
|
||||
// WeatherForecast 向模型返回实际匹配地点和指定日期的预报。
|
||||
type WeatherForecast struct {
|
||||
Location string `json:"location"`
|
||||
Timezone string `json:"timezone"`
|
||||
Days []WeatherDay `json:"days"`
|
||||
}
|
||||
|
||||
type WeatherDay struct {
|
||||
Date string `json:"date"`
|
||||
Condition string `json:"condition"`
|
||||
MinTemperatureC float64 `json:"min_temperature_c"`
|
||||
MaxTemperatureC float64 `json:"max_temperature_c"`
|
||||
PrecipitationChancePct float64 `json:"precipitation_probability_max_pct"`
|
||||
}
|
||||
Reference in new issue
Block a user