大语言模型Prompt实战-喜茶饮品推荐(API接口)

709 阅读2分钟

前言

之前写了一篇Prompt实战 《大语言模型Prompt实战-喜茶饮品推荐》 反响还不错,但主要是通过ChatGPT和ChatGLM的UI界面进行测试的,通过API交互还没有提到。 所以这里就针对API交互提供一个参考案例供大家参考

项目地址

🔗 Github: github.com/ptonlix/dri…

实现

目前项目关键有三个服务 drinkapi drinkrpc gptapi

  1. drinkapi 为新增饮品种类(如喜茶等)、新增饮品和编辑、删除等API请求操作
  2. drinkrpc 为drinkapi的CURD实现
  3. gptapi 向用户提供Chat 的api服务器,负责整合Prompt向openai等服务请求

该三个组件分别在cmd 目录下,采用go-zero框架,通过goctl生成框架代码。

API服务

drinksAPI

1. "获取饮品种类列表"

  1. route definition
  • Url: /v1/drinks/category/list
  • Method: GET
  • Request: -
  • Response: GetCategoryListResp
  1. request definition

  2. response definition

type GetCategoryListResp struct {
	CategoryList []Category `json:"categoryList"`
}

2. "新增饮品种类"

  1. route definition
  • Url: /v1/drinks/category/add
  • Method: POST
  • Request: AddCategoryReq
  • Response: AddCategoryResp
  1. request definition
type AddCategoryReq struct {
	Name string `json:"name"`
	Desc string `json:"desc"`
}
  1. response definition
type AddCategoryResp struct {
	Id int64 `json:"id"`
}

3. "删除饮品种类"

  1. route definition
  • Url: /v1/drinks/category/delete
  • Method: POST
  • Request: DeleteCategoryReq
  • Response: -
  1. request definition
type DeleteCategoryReq struct {
	Id int64 `json:"id"`
}
  1. response definition

4. "更新饮品种类"

  1. route definition
  • Url: /v1/drinks/category/update
  • Method: POST
  • Request: UpdateCategoryReq
  • Response: -
  1. request definition
type UpdateCategoryReq struct {
	Id int64 `json:"id"`
	Name string `json:"name"`
	Desc string `json:"desc"`
}
  1. response definition

5. "获取饮品实例列表"

  1. route definition
  • Url: /v1/drinks/goods/list
  • Method: GET
  • Request: -
  • Response: GetGoodsListResp
  1. request definition

  2. response definition

type GetGoodsListResp struct {
	GoodsList []Goods `json:"list"`
}

6. "新增饮品实例"

  1. route definition
  • Url: /v1/drinks/goods/add
  • Method: POST
  • Request: AddGoodsReq
  • Response: AddGoodsResp
  1. 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"`
}
  1. response definition
type AddGoodsResp struct {
	Id int64 `json:"id"`
}

7. "删除饮品实例"

  1. route definition
  • Url: /v1/drinks/goods/delete
  • Method: POST
  • Request: DeleteGoodsReq
  • Response: -
  1. request definition
type DeleteGoodsReq struct {
	Id int64 `json:"id"`
}
  1. response definition

8. "更新饮品实例"

  1. route definition
  • Url: /v1/drinks/goods/update
  • Method: POST
  • Request: UpdateGoodsReq
  • Response: -
  1. 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"`
}
  1. response definition

gptAPI

1. "获取推荐饮品"

  1. route definition
  • Url: /v1/gpt/drinks/chat
  • Method: POST
  • Request: DrinkGptReq
  • Response: DrinkGptResp
  1. request definition
type DrinkGptReq struct {
	CategoryId int64 `json:"categoryId"`
	Phrases []string `json:"prases"`
}
  1. 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"`
}

结尾

欢迎小伙伴一起讨论~