設定 API Key

openAI API Key



openAI:

如果需要使用openAI的模型,必須先去openai取得API金鑰。取得金鑰後,你可以選擇其中一種方法來匯入key:

1.將KEY放於.env檔案中OPENAI_API_KEY=your api key
1
OPENAI_API_KEY={your api key}
2.設定成環境變數(變數名:OPENAI_API_KEY)
3.在terminal中使用export設定環境變數
1
export OPENAI_API_KEY={your api key}
4.在Python中使用os.environ[‘OPENAI_API_KEY’]=your api key


Azure openAI

如果你想使用Azure openAI,先去azureAI取得base url 和 API key。

OPENAI_API_KEY=your azure key, OPENAI_API_BASE=your Language API base url, OPENAI_API_TYPE=azure, OPENAI_API_VERSION=2023-05-15 寫於.env檔案並放於你要執行akasha的路徑中。

1
2
3
4
5
## .env file
AZURE_API_KEY={your azure key}
AZURE_API_BASE={your Language API base url}
AZURE_API_TYPE=azure
AZURE_API_VERSION=2023-05-15

請記得在Azure openAI Studio部署所有你需要的模型,且部署名稱與模型名稱相同。



gemini API Key

如果你想使用Gemini 模型,請至google aistudioGCP申請API金鑰。

取得金鑰後,你可以選擇其中一種方法來匯入key:

1.將KEY放於.env檔案中GEMINI_API_KEY=your api key
1
GEMINI_API_KEY={your api key}
2.設定成環境變數(變數名:GEMINI_API_KEY)
3.在terminal中使用export設定環境變數
1
export GEMINI_API_KEY={your api key}
4.在Python中使用os.environ[‘GEMINI_API_KEY’]=your api key


anthropic API Key

如果你想使用Gemini 模型,請至anthropic申請API金鑰。

取得金鑰後,你可以選擇其中一種方法來匯入key:

1.將KEY放於.env檔案中ANTHROPIC_API_KEY=your api key
1
ANTHROPIC_API_KEY={your api key}
2.設定成環境變數(變數名:ANTHROPIC_API_KEY)
3.在terminal中使用export設定環境變數
1
export ANTHROPIC_API_KEY={your api key}
4.在Python中使用os.environ[‘ANTHROPIC_API_KEY’]=your api key


遠端API Key

如果你想使用的遠端模型需指定API金鑰

1.將KEY放於.env檔案中RETMOE_API_KEY=your api key
1
REMOTE_API_KEY={your api key}
2.設定成環境變數(變數名:REMOTE_API_KEY)
3.在terminal中使用export設定環境變數
1
export REMOTE_API_KEY={your api key}
4.在Python中使用os.environ[‘REMOTE_API_KEY’]=your api key
1
2
3
import os 
os.environ['REMOTE_API_KEY']="your api key"





LLAMA-2

如果你想使用原版的meta-llama model,並須先去meta-llama去取得授權,並註冊huggingface取得access token,取得授權後才能經由huggingface下載並使用模型。
granted

the account on Hugging Face and the email you use to request access to Meta-Llama must be the same, so that you can download models from Hugging Face once your account is approved.

You should see the Gated model You have been granted access to this model once your account is approved



同樣的,取得huggingface key值後,你可以選擇其中一種方法來匯入key:

1.將KEY放於.env檔案中HF_TOKEN=your api key
1
HF_TOKEN={your api key}
2.設定成環境變數(變數名:HF_TOKEN)
3.在terminal中使用export設定環境變數
1
export HF_TOKEN={your api key}
4.在Python中使用os.environ[‘HF_TOKEN’]=your api key
1
2
3
4
5
#PYTHON3.9
# os.environ['HF_TOKEN']=your api key
import akasha
ak = akasha.Doc_QA()
response = ak.get_response(dir_path, prompt, model="hf:meta-llama/Llama-2-7b-chat-hf")


設定多組不同API KEY

若你需要根據不同模型設定不同的API KEY,在Doc_QA, Model_Eval, Summary Class中皆提供env_file參數,可輸入不同的.env檔來export API KEY
若不存在或為空值,則為預設值(.env)

1
2
3
4
5
6
7
8

import akasha
ak = akasha.Doc_QA("openai:gpt-4o", env_file=".env", verbose=True)
ak.ask_self("日本本州最大的城市是哪裡?")

ak2 = akasha.Doc_QA("openai:gpt-4", env_file=".env2", verbose=True)
ak2.ask_self("日本本州最大的城市是哪裡?")