This commit is contained in:
fuzhongyun 2025-09-09 09:49:15 +08:00
parent 13e387fa94
commit 160bb86fb0
9 changed files with 300 additions and 20 deletions

Binary file not shown.

View File

@ -4,7 +4,7 @@ server:
ollama: ollama:
base_url: "http://localhost:11434" base_url: "http://localhost:11434"
model: "qwen:8b" model: "qwen3:8b"
timeout: 30s timeout: 30s
tools: tools:

View File

@ -1,4 +1,4 @@
// Package docs GENERATED BY SWAG; DO NOT EDIT // Package docs Code generated by swaggo/swag. DO NOT EDIT
package docs package docs
import "github.com/swaggo/swag" import "github.com/swaggo/swag"
@ -104,10 +104,6 @@ const docTemplate = `{
"message": { "message": {
"type": "string", "type": "string",
"example": "北京今天天气怎么样?" "example": "北京今天天气怎么样?"
},
"model": {
"type": "string",
"example": "llama2"
} }
} }
}, },
@ -187,8 +183,10 @@ var SwaggerInfo = &swag.Spec{
Description: "智能路由调度系统API文档", Description: "智能路由调度系统API文档",
InfoInstanceName: "swagger", InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate, SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
} }
func init() { func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
} }

168
docs/swagger.json Normal file
View File

@ -0,0 +1,168 @@
{
"swagger": "2.0",
"info": {
"description": "智能路由调度系统API文档",
"title": "AI Scheduler API",
"termsOfService": "http://swagger.io/terms/",
"contact": {
"name": "API Support",
"url": "http://www.swagger.io/support",
"email": "support@swagger.io"
},
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
},
"version": "1.0"
},
"host": "localhost:8080",
"basePath": "/",
"paths": {
"/api/v1/chat": {
"post": {
"description": "发送消息给AI助手支持工具调用",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"chat"
],
"summary": "智能聊天",
"parameters": [
{
"description": "聊天请求",
"name": "request",
"in": "body",
"required": true,
"schema": {
"$ref": "#/definitions/handlers.ChatRequest"
}
}
],
"responses": {
"200": {
"description": "聊天响应",
"schema": {
"$ref": "#/definitions/handlers.ChatResponse"
}
},
"400": {
"description": "请求错误",
"schema": {
"$ref": "#/definitions/handlers.ErrorResponse"
}
},
"500": {
"description": "服务器错误",
"schema": {
"$ref": "#/definitions/handlers.ErrorResponse"
}
}
}
}
},
"/health": {
"get": {
"description": "检查服务是否正常运行",
"produces": [
"application/json"
],
"tags": [
"system"
],
"summary": "健康检查",
"responses": {
"200": {
"description": "OK",
"schema": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
}
}
}
},
"definitions": {
"handlers.ChatRequest": {
"type": "object",
"required": [
"message"
],
"properties": {
"message": {
"type": "string",
"example": "北京今天天气怎么样?"
}
}
},
"handlers.ChatResponse": {
"type": "object",
"properties": {
"finished": {
"type": "boolean",
"example": true
},
"message": {
"type": "string",
"example": "北京今天天气晴朗温度15.3°C"
},
"tool_calls": {
"type": "array",
"items": {
"$ref": "#/definitions/handlers.ToolCallResponse"
}
}
}
},
"handlers.ErrorResponse": {
"type": "object",
"properties": {
"code": {
"type": "integer",
"example": 400
},
"error": {
"type": "string",
"example": "Invalid request"
},
"message": {
"type": "string",
"example": "请求参数错误"
}
}
},
"handlers.FunctionCallResponse": {
"type": "object",
"properties": {
"arguments": {},
"name": {
"type": "string",
"example": "get_weather"
}
}
},
"handlers.ToolCallResponse": {
"type": "object",
"properties": {
"function": {
"$ref": "#/definitions/handlers.FunctionCallResponse"
},
"id": {
"type": "string",
"example": "call_1"
},
"result": {},
"type": {
"type": "string",
"example": "function"
}
}
}
}
}

114
docs/swagger.yaml Normal file
View File

@ -0,0 +1,114 @@
basePath: /
definitions:
handlers.ChatRequest:
properties:
message:
example: 北京今天天气怎么样?
type: string
required:
- message
type: object
handlers.ChatResponse:
properties:
finished:
example: true
type: boolean
message:
example: 北京今天天气晴朗温度15.3°C
type: string
tool_calls:
items:
$ref: '#/definitions/handlers.ToolCallResponse'
type: array
type: object
handlers.ErrorResponse:
properties:
code:
example: 400
type: integer
error:
example: Invalid request
type: string
message:
example: 请求参数错误
type: string
type: object
handlers.FunctionCallResponse:
properties:
arguments: {}
name:
example: get_weather
type: string
type: object
handlers.ToolCallResponse:
properties:
function:
$ref: '#/definitions/handlers.FunctionCallResponse'
id:
example: call_1
type: string
result: {}
type:
example: function
type: string
type: object
host: localhost:8080
info:
contact:
email: support@swagger.io
name: API Support
url: http://www.swagger.io/support
description: 智能路由调度系统API文档
license:
name: Apache 2.0
url: http://www.apache.org/licenses/LICENSE-2.0.html
termsOfService: http://swagger.io/terms/
title: AI Scheduler API
version: "1.0"
paths:
/api/v1/chat:
post:
consumes:
- application/json
description: 发送消息给AI助手支持工具调用
parameters:
- description: 聊天请求
in: body
name: request
required: true
schema:
$ref: '#/definitions/handlers.ChatRequest'
produces:
- application/json
responses:
"200":
description: 聊天响应
schema:
$ref: '#/definitions/handlers.ChatResponse'
"400":
description: 请求错误
schema:
$ref: '#/definitions/handlers.ErrorResponse'
"500":
description: 服务器错误
schema:
$ref: '#/definitions/handlers.ErrorResponse'
summary: 智能聊天
tags:
- chat
/health:
get:
description: 检查服务是否正常运行
produces:
- application/json
responses:
"200":
description: OK
schema:
additionalProperties:
type: string
type: object
summary: 健康检查
tags:
- system
swagger: "2.0"

4
go.mod
View File

@ -9,7 +9,7 @@ require (
github.com/spf13/viper v1.17.0 github.com/spf13/viper v1.17.0
github.com/swaggo/files v1.0.1 github.com/swaggo/files v1.0.1
github.com/swaggo/gin-swagger v1.6.1 github.com/swaggo/gin-swagger v1.6.1
github.com/swaggo/swag v1.8.12 github.com/swaggo/swag v1.16.6
) )
require ( require (
@ -57,7 +57,9 @@ require (
golang.org/x/arch v0.8.0 // indirect golang.org/x/arch v0.8.0 // indirect
golang.org/x/crypto v0.36.0 // indirect golang.org/x/crypto v0.36.0 // indirect
golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect golang.org/x/exp v0.0.0-20250218142911-aa4b98e5adaa // indirect
golang.org/x/mod v0.23.0 // indirect
golang.org/x/net v0.38.0 // indirect golang.org/x/net v0.38.0 // indirect
golang.org/x/sync v0.12.0 // indirect
golang.org/x/sys v0.31.0 // indirect golang.org/x/sys v0.31.0 // indirect
golang.org/x/text v0.23.0 // indirect golang.org/x/text v0.23.0 // indirect
golang.org/x/tools v0.30.0 // indirect golang.org/x/tools v0.30.0 // indirect

4
go.sum
View File

@ -257,8 +257,8 @@ github.com/swaggo/files v1.0.1 h1:J1bVJ4XHZNq0I46UU90611i9/YzdrF7x92oX1ig5IdE=
github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg= github.com/swaggo/files v1.0.1/go.mod h1:0qXmMNH6sXNf+73t65aKeB+ApmgxdnkQzVTAj2uaMUg=
github.com/swaggo/gin-swagger v1.6.1 h1:Ri06G4gc9N4t4k8hekMigJ9zKTFSlqj/9paAQCQs7cY= github.com/swaggo/gin-swagger v1.6.1 h1:Ri06G4gc9N4t4k8hekMigJ9zKTFSlqj/9paAQCQs7cY=
github.com/swaggo/gin-swagger v1.6.1/go.mod h1:LQ+hJStHakCWRiK/YNYtJOu4mR2FP+pxLnILT/qNiTw= github.com/swaggo/gin-swagger v1.6.1/go.mod h1:LQ+hJStHakCWRiK/YNYtJOu4mR2FP+pxLnILT/qNiTw=
github.com/swaggo/swag v1.8.12 h1:pctzkNPu0AlQP2royqX3apjKCQonAnf7KGoxeO4y64w= github.com/swaggo/swag v1.16.6 h1:qBNcx53ZaX+M5dxVyTrgQ0PJ/ACK+NzhwcbieTt+9yI=
github.com/swaggo/swag v1.8.12/go.mod h1:lNfm6Gg+oAq3zRJQNEMBE66LIJKM44mxFqhEEgy2its= github.com/swaggo/swag v1.16.6/go.mod h1:ngP2etMK5a0P3QBizic5MEwpRmluJZPHjXcMoj4Xesg=
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI= github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE=

View File

@ -22,22 +22,21 @@ func NewChatHandler(routerService types.RouterService) *ChatHandler {
// ChatRequest HTTP聊天请求 // ChatRequest HTTP聊天请求
type ChatRequest struct { type ChatRequest struct {
Message string `json:"message" binding:"required" example:"北京今天天气怎么样?"` Message string `json:"message" binding:"required" example:"北京今天天气怎么样?"`
Model string `json:"model,omitempty" example:"llama2"`
} }
// ChatResponse HTTP聊天响应 // ChatResponse HTTP聊天响应
type ChatResponse struct { type ChatResponse struct {
Message string `json:"message" example:"北京今天天气晴朗温度15.3°C"` Message string `json:"message" example:"北京今天天气晴朗温度15.3°C"`
ToolCalls []ToolCallResponse `json:"tool_calls,omitempty"` ToolCalls []ToolCallResponse `json:"tool_calls,omitempty"`
Finished bool `json:"finished" example:"true"` Finished bool `json:"finished" example:"true"`
} }
// ToolCallResponse 工具调用响应 // ToolCallResponse 工具调用响应
type ToolCallResponse struct { type ToolCallResponse struct {
ID string `json:"id" example:"call_1"` ID string `json:"id" example:"call_1"`
Type string `json:"type" example:"function"` Type string `json:"type" example:"function"`
Function FunctionCallResponse `json:"function"` Function FunctionCallResponse `json:"function"`
Result interface{} `json:"result,omitempty"` Result interface{} `json:"result,omitempty"`
} }
// FunctionCallResponse 函数调用响应 // FunctionCallResponse 函数调用响应
@ -78,7 +77,6 @@ func (h *ChatHandler) Chat(c *gin.Context) {
// 转换为服务层请求 // 转换为服务层请求
serviceReq := &types.ChatRequest{ serviceReq := &types.ChatRequest{
Message: req.Message, Message: req.Message,
Model: req.Model,
} }
// 调用路由服务 // 调用路由服务
@ -126,7 +124,7 @@ func (h *ChatHandler) Chat(c *gin.Context) {
// @Router /health [get] // @Router /health [get]
func (h *ChatHandler) Health(c *gin.Context) { func (h *ChatHandler) Health(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{ c.JSON(http.StatusOK, gin.H{
"status": "ok", "status": "ok",
"service": "ai-scheduler", "service": "ai-scheduler",
}) })
} }

BIN
server

Binary file not shown.