0%

FAST API

API 使用手冊

akasha 提供了一個 RESTful API 伺服器,允許使用者通過 HTTP 請求來調用其核心功能,包括摘要 (summary)、文件檢索與生成 (RAG)、提問 (ask)、以及網頁搜尋 (websearch)。


啟動 API 伺服器

在終端啟動 API 伺服器:

1
akasha api -p 8000 -h 127.0.0.1
  • -p:指定伺服器的埠號(預設為 8000)。
  • -h:指定伺服器的主機地址(預設為 127.0.0.1)。

API 功能

1. 提問 (/ask)

使用語言模型回答問題,並可基於提供的內容進行回答。

範例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import requests

ask_data = {
"prompt": "太陽能電池技術?",
"info": "太陽能電池技術5塊錢",
"model": "openai:gpt-3.5-turbo",
"system_prompt": "",
"temperature": 0.0,
"env_config": {
"OPENAI_API_KEY": "your_openai_key",
},
}

response = requests.post("http://127.0.0.1:8000/ask", json=ask_data).json()
print(response)
請求參數
  • prompt: 使用者的問題。
  • info: 提供的內容,可以是文字、文件路徑或網址。
  • model: 使用的語言模型,例如 "openai:gpt-3.5-turbo"
  • system_prompt: 指示語言模型的輸出格式需求。
  • temperature: 語言模型的隨機性參數。
  • env_config: 環境變數配置,例如 API 金鑰。

2. 文件檢索與生成 (/RAG)

基於提供的文件資料來源,檢索相關內容並生成回答。

範例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
rag_data = {
"data_source": "docs/mic/",
"prompt": "工業4.0",
"chunk_size": 1000,
"model": "openai:gpt-3.5-turbo",
"embedding_model": "openai:text-embedding-ada-002",
"threshold": 0.1,
"search_type": "auto",
"system_prompt": "",
"max_input_tokens": 3000,
"temperature": 0.0,
"env_config": {
"OPENAI_API_KEY": "your_openai_key",
},
}

response = requests.post("http://127.0.0.1:8000/RAG", json=rag_data).json()
print(response)
請求參數
  • data_source: 文件資料來源,可以是目錄或文件路徑。
  • prompt: 使用者的問題。
  • chunk_size: 文件分塊大小。
  • model: 使用的語言模型。
  • embedding_model: 使用的嵌入模型。
  • threshold: 檢索的相似性閾值。
  • search_type: 檢索類型,例如 "auto"
  • system_prompt: 指示語言模型的輸出格式需求。
  • max_input_tokens: 單次輸入的最大 token 數。
  • temperature: 語言模型的隨機性參數。
  • env_config: 環境變數配置。

3. 摘要 (/summary)

對提供的內容進行摘要,支援多種摘要方法。

範例
1
2
3
4
5
6
7
8
9
10
11
12
13
summary_data = {
"content": "docs/2.pdf",
"model": "openai:gpt-3.5-turbo",
"summary_type": "reduce_map",
"summary_len": 500,
"system_prompt": "用中文做500字摘要",
"env_config": {
"OPENAI_API_KEY": "your_openai_key",
},
}

response = requests.post("http://127.0.0.1:8000/summary", json=summary_data).json()
print(response)
請求參數
  • content: 需要摘要的內容,可以是文件路徑、網址或純文字。
  • summary_type: 摘要方法,例如 "map_reduce""refine"
  • summary_len: 摘要的建議長度。
  • model: 使用的語言模型。
  • system_prompt: 指示語言模型的輸出格式需求。
  • env_config: 環境變數配置。

4. 網頁搜尋 (/websearch)

使用搜尋引擎搜尋問題相關的資訊,並使用語言模型整合回答。

範例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
websearch_data = {
"prompt": "太陽能電池技術?",
"model": "openai:gpt-3.5-turbo",
"system_prompt": "",
"temperature": 0.0,
"env_config": {
"SERPER_API_KEY": "your_serper_key",
},
"search_engine": "serper",
"search_num": 5,
}

response = requests.post("http://127.0.0.1:8000/websearch", json=websearch_data).json()
print(response)
請求參數
  • prompt: 使用者的問題。
  • model: 使用的語言模型。
  • system_prompt: 指示語言模型的輸出格式需求。
  • temperature: 語言模型的隨機性參數。
  • search_engine: 搜尋引擎,例如 "serper""brave""wiki"
  • search_num: 搜尋結果的數量。
  • env_config: 環境變數配置。

環境變數配置

在請求中可以通過 env_config 傳遞環境變數,例如:

1
2
3
4
5
{
"OPENAI_API_KEY": "your_openai_key",
"SERPER_API_KEY": "your_serper_key",
"BRAVE_API_KEY": "your_brave_key"
}

API 返回格式

所有 API 返回的 JSON 格式如下:

1
2
3
4
5
6
7
8
9
{
"response": "回答內容或摘要",
"status": "success 或 fail",
"logs": {
"執行過程的詳細日誌"
},
"timestamp": "執行時間戳",
"warnings": ["警告信息"]
}

相關連結

  • 文件搜尋
  • 設定語言模型