Skip to content

Commit 77694d6

Browse files
committed
feat: llm friendly style response
1 parent 8b74041 commit 77694d6

File tree

11 files changed

+193
-91
lines changed

11 files changed

+193
-91
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Based on Playwright and DuckDuckGo's search engine, it encapsulates to provide s
2323

2424
Simple, lightweight, reliable, Docker deployable, easy to maintain.
2525

26+
Large Language Model (LLM) Friendly. Support Plain Text Response.
27+
2628
## Status
2729

2830
> Experimentally under development and not recommended for use in production environments.
@@ -47,13 +49,14 @@ docker run -d -p 8080:8080 --restart=unless-stopped --name lessapi-duckduckgo le
4749

4850
- keyword: Search keyword (required)
4951
- region: Region (optional) such as en-US, fr-FR, zh-CN, ru-RU, etc. Default is en-US
50-
- maxCount: Maximum number of results returned (optional) Default is 20
51-
- viaProxyUrl: The address of the proxy used by the browser (optional) e.g., http://proxy.server:3000 Default is empty
52+
- maxCount: Maximum number of results returned. (optional) Default is 20
53+
- viaProxyUrl: The address of the proxy used by the browser. e.g., http://proxy.server:3000 (optional) Default is empty
54+
- llmStyle: Whether to use Large Language Model (LLM) Friendly style response. e.g. 1, 0 (optional) Default is 0
5255

5356
**Request Example:**
5457

5558
```shell
56-
curl 'http://127.0.0.1:8080/search/text?keyword=USA&maxCount=10'
59+
curl 'http://127.0.0.1:8080/search/text?keyword=lessapi&maxCount=10'
5760
```
5861

5962
```shell

docs/zhs/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ LessAPI-DuckDuckGo 是一个搜索引擎API服务。
2626

2727
简单、轻量、可靠、Docker部署、易于维护。
2828

29+
大型语言模型(LLM)友好。支持纯文本响应。
30+
2931
## 状态
3032

3133
> 实验性地开发中,不建议在生产环境中使用。
@@ -52,11 +54,16 @@ docker run -d -p 8080:8080 --restart=unless-stopped --name lessapi-duckduckgo le
5254
- region: 地区(选填) en-US, fr-FR, zh-CN, ru-RU, 等 默认值 en-US
5355
- maxCount: 最大返回数量(选填) 默认值 20
5456
- viaProxyUrl: 浏览器使用代理的地址(选填) 如 http://proxy.server:3000 默认值 空
57+
- llmStyle: 是否使用大型语言模型(LLM)友好风格响应(选填) 1, 0 默认值 0
5558

5659
**请求示例:**
5760

5861
```shell
59-
curl 'http://127.0.0.1:8080/search/text?keyword=hello&maxCount=2'
62+
curl 'http://127.0.0.1:8080/search/text?keyword=lessapi&maxCount=10'
63+
```
64+
65+
```shell
66+
curl 'http://127.0.0.1:8080/search/text?keyword=lessapi&maxCount=99&viaProxyUrl=http://proxy.server:3000'
6067
```
6168

6269
**响应示例:**

docs/zht/README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ LessAPI-DuckDuckGo 是一個搜索引擎API服務。
2727

2828
簡單、輕量、可靠、Docker部署、易於維護。
2929

30+
大型語言模型(LLM)友好。支持純文本響應。
31+
3032
## 狀態
3133

3234
> 專案正在實驗性地開發中,不建議在生產環境中使用。
3335
36+
## 部署
37+
38+
使用Docker只需要一個命令即可將服務部署到8080端口。
39+
40+
```shell
41+
docker run -d -p 8080:8080 --restart=unless-stopped --name lessapi-duckduckgo lessapi/lessapi-duckduckgo:v0.0.2
42+
```
43+
3444
## 使用
3545

3646
### OpenAPI標準文檔 (Swagger 3.0)
@@ -45,11 +55,16 @@ LessAPI-DuckDuckGo 是一個搜索引擎API服務。
4555
- region: 地區(選填) en-US, fr-FR, zh-CN, ru-RU, 等 預設值 en-US
4656
- maxCount: 最大返回數量(選填) 預設值 20
4757
- viaProxyUrl: 瀏覽器使用代理的地址(選填) 如 http://proxy.server:3000 預設值 空
58+
- llmStyle: 是否使用大型語言模型(LLM)友好風格響應(選填) 1, 0 預設值 0
4859

4960
**請求示例:**
5061

5162
```shell
52-
curl 'http://127.0.0.1:8080/search/text?keyword=hello&maxCount=2'
63+
curl 'http://127.0.0.1:8080/search/text?keyword=lessapi&maxCount=10'
64+
```
65+
66+
```shell
67+
curl 'http://127.0.0.1:8080/search/text?keyword=lessapi&maxCount=99&viaProxyUrl=http://proxy.server:3000'
5368
```
5469

5570
**響應示例:**

internal/handles/index_handle.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,20 @@ import (
88
)
99

1010
func IndexHandle(c *gin.Context) {
11-
resp := gin.H{
12-
"server": lessapi_duckduckgo.ServerName,
13-
"github": lessapi_duckduckgo.ProjectGithub,
14-
"version": lessapi_duckduckgo.Version,
11+
resp := IndexResponse{
12+
Server: lessapi_duckduckgo.ServerName,
13+
Github: lessapi_duckduckgo.ProjectGithub,
14+
Version: lessapi_duckduckgo.Version,
1515
}
16-
c.JSON(200, utils.BuildApiSuccessData(resp))
16+
utils.SendResponse(c, utils.BuildApiSuccessData(resp))
17+
}
18+
19+
type IndexResponse struct {
20+
Server string `json:"server"`
21+
Github string `json:"github"`
22+
Version string `json:"version"`
23+
}
24+
25+
func (i IndexResponse) ToLLMStyle() string {
26+
return "Server: " + i.Server + "\nGithub: " + i.Github + "\nVersion: " + i.Version
1727
}

internal/handles/list_language_handle.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,24 @@ func ListLanguageHandle(c *gin.Context) {
1818
// parse request params
1919
var payload types.ListLanguagePayload
2020
if err := c.ShouldBindQuery(&payload); err != nil {
21-
c.JSON(200, utils.BuildApiError("invalid_params", "invalid request params"))
21+
utils.SendResponse(c, utils.BuildApiError("invalid_params", "invalid request params"))
2222
return
2323
}
2424

2525
// get from cache
2626
languages, found := languagesCache.Get("languages")
2727
if found {
28-
c.JSON(200, utils.BuildApiSuccessData(types.ListLanguageResponse{
28+
resp := types.ListLanguageResponse{
2929
Languages: languages.([]types.LanguageType),
30-
}))
30+
}
31+
utils.SendResponse(c, utils.BuildApiSuccessData(resp))
3132
return
3233
}
3334

3435
// search text
3536
resp, err := searchs.ListLanguage(payload)
3637
if err != nil {
37-
c.JSON(200, utils.BuildApiError("search_error", err.Error()))
38+
utils.SendResponse(c, utils.BuildApiError("fetch_error", err.Error()))
3839
return
3940
}
4041

@@ -43,5 +44,5 @@ func ListLanguageHandle(c *gin.Context) {
4344
}
4445

4546
// return response
46-
c.JSON(200, utils.BuildApiSuccessData(resp))
47+
utils.SendResponse(c, utils.BuildApiSuccessData(resp))
4748
}

internal/handles/text_handle.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
package handles
22

33
import (
4-
"github.com/gin-gonic/gin"
5-
64
"github.com/lessapidev/lessapi-duckduckgo/internal/searchs"
75
"github.com/lessapidev/lessapi-duckduckgo/internal/types"
86
"github.com/lessapidev/lessapi-duckduckgo/internal/utils"
7+
8+
"github.com/gin-gonic/gin"
99
)
1010

1111
// SearchTextHandle search text
1212
func SearchTextHandle(c *gin.Context) {
1313
// parse request params
1414
var payload types.SearchTextPayload
1515
if err := c.ShouldBindQuery(&payload); err != nil {
16-
c.JSON(200, utils.BuildApiError("invalid_params", "invalid request params"))
16+
utils.SendResponse(c, utils.BuildApiError("invalid_params", "invalid request params"))
1717
return
1818
}
1919

2020
// search text
2121
resp, err := searchs.SearchText(payload)
2222
if err != nil {
23-
c.JSON(200, utils.BuildApiError("search_error", err.Error()))
23+
utils.SendResponse(c, utils.BuildApiError("search_error", err.Error()))
2424
return
2525
}
2626

2727
// return response
28-
c.JSON(200, utils.BuildApiSuccessData(resp))
28+
utils.SendResponse(c, utils.BuildApiSuccessData(resp))
2929
}

internal/types/responses.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package types
2+
3+
import "fmt"
4+
5+
type SearchTextPayload struct {
6+
// Keywords to search (required)
7+
Keyword string `json:"keyword" form:"keyword" validate:"required"`
8+
// Region to search (optional) (default "en-US")
9+
Region string `json:"region" form:"region"`
10+
// Time limit to search (optional) ( "" default all , "d" past day, "w" past week, "m" past month, "y" past year)
11+
TimeLimit string `json:"timeLimit" form:"timeLimit"`
12+
// Max count of search results (optional) (default 20)
13+
MaxCount int `json:"maxCount" form:"maxCount"`
14+
// Proxy url to use (optional)
15+
ViaProxyUrl string `json:"viaProxyUrl" form:"viaProxyUrl"`
16+
}
17+
18+
type SearchTextResponse struct {
19+
Results []SearchTextResultItem `json:"results"`
20+
}
21+
22+
func (s SearchTextResponse) ToLLMStyle() string {
23+
var resultStr string
24+
for i, item := range s.Results {
25+
resultStr += fmt.Sprintf("# %d. %s\nURL:%s\nAbstract:%s\n\n", i+1, item.Title, item.Url, item.Description)
26+
}
27+
return resultStr
28+
}
29+
30+
type SearchTextResultItem struct {
31+
Order int `json:"order"` // order of the result
32+
Title string `json:"title"` // title of the result
33+
Url string `json:"url"` // url of the result
34+
Description string `json:"description"` // description of the result
35+
}
36+
37+
// -----------------------------------------------------------
38+
39+
type LanguageType struct {
40+
Code string `json:"code"`
41+
Name string `json:"name"`
42+
}
43+
44+
type ListLanguagePayload struct {
45+
// Proxy url to use (optional)
46+
ViaProxyUrl string `json:"viaProxyUrl" form:"viaProxyUrl"`
47+
}
48+
49+
type ListLanguageResponse struct {
50+
Languages []LanguageType `json:"languages"`
51+
}
52+
53+
func (l ListLanguageResponse) ToLLMStyle() string {
54+
var resultStr string
55+
for _, item := range l.Languages {
56+
resultStr += fmt.Sprintf("%s (%s)\n", item.Name, item.Code)
57+
}
58+
return resultStr
59+
}

internal/types/types.go

Lines changed: 9 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,17 @@
11
package types
22

3-
type ApiResponse[T any] struct {
4-
Code string `json:"code"`
5-
Message *string `json:"message,omitempty"`
6-
Data T `json:"data,omitempty"`
3+
type IResponse interface {
4+
ToLLMStyle() string
75
}
86

9-
type SearchTextPayload struct {
10-
// Keywords to search (required)
11-
Keyword string `json:"keyword" form:"keyword" validate:"required"`
12-
// Region to search (optional) (default "en-US")
13-
Region string `json:"region" form:"region"`
14-
// Time limit to search (optional) ( "" default all , "d" past day, "w" past week, "m" past month, "y" past year)
15-
TimeLimit string `json:"timeLimit" form:"timeLimit"`
16-
// Max count of search results (optional) (default 20)
17-
MaxCount int `json:"maxCount" form:"maxCount"`
18-
// Proxy url to use (optional)
19-
ViaProxyUrl string `json:"viaProxyUrl" form:"viaProxyUrl"`
7+
type ApiResponse struct {
8+
Code string `json:"code"`
9+
Message *string `json:"message,omitempty"`
10+
Data IResponse `json:"data,omitempty"`
2011
}
2112

22-
type SearchTextResponse struct {
23-
Results []SearchTextResultItem `json:"results"`
24-
}
25-
26-
type SearchTextResultItem struct {
27-
Order int `json:"order"` // order of the result
28-
Title string `json:"title"` // title of the result
29-
Url string `json:"url"` // url of the result
30-
Description string `json:"description"` // description of the result
31-
}
32-
33-
type LanguageType struct {
34-
Code string `json:"code"`
35-
Name string `json:"name"`
36-
}
37-
38-
type ListLanguagePayload struct {
39-
// Proxy url to use (optional)
40-
ViaProxyUrl string `json:"viaProxyUrl" form:"viaProxyUrl"`
41-
}
13+
type EmptyResponse struct{}
4214

43-
type ListLanguageResponse struct {
44-
Languages []LanguageType `json:"languages"`
15+
func (e EmptyResponse) ToLLMStyle() string {
16+
return "success"
4517
}

internal/utils/api_util.go

Lines changed: 0 additions & 33 deletions
This file was deleted.

internal/utils/env_util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package utils
22

33
import (
4-
"github.com/lessapidev/lessapi-duckduckgo/internal/envs"
54
"os"
5+
6+
"github.com/lessapidev/lessapi-duckduckgo/internal/envs"
67
)
78

89
// GetEnvOrDefault returns the value of the environment variable with the given key.

0 commit comments

Comments
 (0)