Memorize
微調 LLM 本身以使用非監督式學習記憶資訊。
此工具需要支援微調的 LLM。 目前,僅支援 langchain.llms import GradientLLM
。
匯入 (Imports)
import os
from langchain.agents import AgentExecutor, AgentType, initialize_agent, load_tools
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemory
from langchain_community.llms import GradientLLM
API 參考:AgentExecutor | AgentType | initialize_agent | load_tools | LLMChain | ConversationBufferMemory | GradientLLM
設定環境 API 金鑰 (Set the Environment API Key)
請務必從 Gradient AI 取得您的 API 金鑰。 您將獲得 10 美元的免費額度來測試和微調不同的模型。
from getpass import getpass
if not os.environ.get("GRADIENT_ACCESS_TOKEN", None):
# Access token under https://auth.gradient.ai/select-workspace
os.environ["GRADIENT_ACCESS_TOKEN"] = getpass("gradient.ai access token:")
if not os.environ.get("GRADIENT_WORKSPACE_ID", None):
# `ID` listed in `$ gradient workspace list`
# also displayed after login at at https://auth.gradient.ai/select-workspace
os.environ["GRADIENT_WORKSPACE_ID"] = getpass("gradient.ai workspace id:")
if not os.environ.get("GRADIENT_MODEL_ADAPTER_ID", None):
# `ID` listed in `$ gradient model list --workspace-id "$GRADIENT_WORKSPACE_ID"`
os.environ["GRADIENT_MODEL_ID"] = getpass("gradient.ai model id:")
選用:驗證您的環境變數 GRADIENT_ACCESS_TOKEN
和 GRADIENT_WORKSPACE_ID
以取得目前部署的模型。
建立 GradientLLM
實例 (Create the GradientLLM
instance)
您可以指定不同的參數,例如模型名稱、產生的最大 token 數量、溫度等。
llm = GradientLLM(
model_id=os.environ["GRADIENT_MODEL_ID"],
# # optional: set new credentials, they default to environment variables
# gradient_workspace_id=os.environ["GRADIENT_WORKSPACE_ID"],
# gradient_access_token=os.environ["GRADIENT_ACCESS_TOKEN"],
)
載入工具 (Load tools)
tools = load_tools(["memorize"], llm=llm)
啟動 Agent (Initiate the Agent)
agent = initialize_agent(
tools,
llm,
agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION,
verbose=True,
# memory=ConversationBufferMemory(memory_key="chat_history", return_messages=True),
)
執行 Agent (Run the agent)
要求 Agent 記住一段文字。
agent.run(
"Please remember the fact in detail:\nWith astonishing dexterity, Zara Tubikova set a world record by solving a 4x4 Rubik's Cube variation blindfolded in under 20 seconds, employing only their feet."
)
[1m> Entering new AgentExecutor chain...[0m
[32;1m[1;3mI should memorize this fact.
Action: Memorize
Action Input: Zara T[0m
Observation: [36;1m[1;3mTrain complete. Loss: 1.6853971333333335[0m
Thought:[32;1m[1;3mI now know the final answer.
Final Answer: Zara Tubikova set a world[0m
[1m> Finished chain.[0m
'Zara Tubikova set a world'