前言
之前写了一篇Prompt实战 《大语言模型Prompt实战-喜茶饮品推荐》 反响还不错,但主要是通过ChatGPT和ChatGLM的UI界面进行测试的,通过API交互还没有提到。 所以这里就针对API交互提供一个参考案例供大家参考
项目地址
🔗 Github: github.com/ptonlix/dri…
实现
目前项目关键有三个服务 drinkapi drinkrpc gptapi
- drinkapi 为新增饮品种类(如喜茶等)、新增饮品和编辑、删除等API请求操作
- drinkrpc 为drinkapi的CURD实现
- gptapi 向用户提供Chat 的api服务器,负责整合Prompt向openai等服务请求
该三个组件分别在cmd 目录下,采用go-zero框架,通过goctl生成框架代码。
API服务
drinksAPI
1. "获取饮品种类列表"
- route definition
- Url: /v1/drinks/category/list
- Method: GET
- Request:
- - Response:
GetCategoryListResp
-
request definition
-
response definition
type GetCategoryListResp struct {
CategoryList []Category `json:"categoryList"`
}
2. "新增饮品种类"
- route definition
- Url: /v1/drinks/category/add
- Method: POST
- Request:
AddCategoryReq - Response:
AddCategoryResp
- request definition
type AddCategoryReq struct {
Name string `json:"name"`
Desc string `json:"desc"`
}
- response definition
type AddCategoryResp struct {
Id int64 `json:"id"`
}
3. "删除饮品种类"
- route definition
- Url: /v1/drinks/category/delete
- Method: POST
- Request:
DeleteCategoryReq - Response:
-
- request definition
type DeleteCategoryReq struct {
Id int64 `json:"id"`
}
- response definition
4. "更新饮品种类"
- route definition
- Url: /v1/drinks/category/update
- Method: POST
- Request:
UpdateCategoryReq - Response:
-
- request definition
type UpdateCategoryReq struct {
Id int64 `json:"id"`
Name string `json:"name"`
Desc string `json:"desc"`
}
- response definition
5. "获取饮品实例列表"
- route definition
- Url: /v1/drinks/goods/list
- Method: GET
- Request:
- - Response:
GetGoodsListResp
-
request definition
-
response definition
type GetGoodsListResp struct {
GoodsList []Goods `json:"list"`
}
6. "新增饮品实例"
- route definition
- Url: /v1/drinks/goods/add
- Method: POST
- Request:
AddGoodsReq - Response:
AddGoodsResp
- request definition
type AddGoodsReq struct {
CategoryId int64 `json:"categoryId"`
Name string `json:"name"`
Ingredients string `json:"ingredients"`
Tea string `json:"tea"`
CupCapacity string `json:"cup_capacity"`
}
- response definition
type AddGoodsResp struct {
Id int64 `json:"id"`
}
7. "删除饮品实例"
- route definition
- Url: /v1/drinks/goods/delete
- Method: POST
- Request:
DeleteGoodsReq - Response:
-
- request definition
type DeleteGoodsReq struct {
Id int64 `json:"id"`
}
- response definition
8. "更新饮品实例"
- route definition
- Url: /v1/drinks/goods/update
- Method: POST
- Request:
UpdateGoodsReq - Response:
-
- request definition
type UpdateGoodsReq struct {
Id int64 `json:"id"`
CategoryId int64 `json:"categoryId"`
Name string `json:"name"`
Ingredients string `json:"ingredients"`
Tea string `json:"tea"`
CupCapacity string `json:"cup_capacity"`
}
- response definition
gptAPI
1. "获取推荐饮品"
- route definition
- Url: /v1/gpt/drinks/chat
- Method: POST
- Request:
DrinkGptReq - Response:
DrinkGptResp
- request definition
type DrinkGptReq struct {
CategoryId int64 `json:"categoryId"`
Phrases []string `json:"prases"`
}
- response definition
type DrinkGptResp struct {
Drink Goods `json:"drink"`
}
type Goods struct {
Id int64 `json:"id"`
CategoryId int64 `json:"categoryId"`
Name string `json:"name"`
Ingredients string `json:"ingredients"`
Tea string `json:"tea"`
CupCapacity string `json:"cup_capacity"`
Reason string `json:"reason"`
}
结尾
欢迎小伙伴一起讨论~