網(wǎng)上有很多關(guān)于pos機(jī)模型素材,Vicuna大模型AI代理開發(fā)實(shí)戰(zhàn)的知識,也有很多人為大家解答關(guān)于pos機(jī)模型素材的問題,今天pos機(jī)之家(www.shineka.com)為大家整理了關(guān)于這方面的知識,讓我們一起來看下吧!
本文目錄一覽:
pos機(jī)模型素材
由于最近對 LLM 的所有炒作,我決定嘗試使用最近的開源工具并編寫一個(gè) AI 代理,它可以根據(jù)提示編寫和執(zhí)行 Python 代碼。
推薦:用 NSDT設(shè)計(jì)器 快速搭建可編程3D場景。
在本文中,我將描述整個(gè)過程并提供一些資源來幫助你入門。 我們將討論如何獲取模型,如何使用Vicuna創(chuàng)建本地推理服務(wù)并將此服務(wù)與Langchain鏈接,以及如何從Langchain運(yùn)行AI Agent API等內(nèi)容。
下面是我的實(shí)驗(yàn)環(huán)境:
Langchain,一個(gè)用于構(gòu)建由 LLM 提供支持的應(yīng)用程序的庫Vicuna 7B 模型,在 GPU 上本地運(yùn)行fastchat 源代碼,作為我自己的基礎(chǔ),與上面的鏈接相同。FastAPI 本地服務(wù)器配備 RTX-3090 GPU 的臺式機(jī),在開發(fā) AI 代理幾個(gè)小時(shí)后,VRAM 使用量約為 19GB。超過 16GB 的 RAM 可用于將 llama 模型轉(zhuǎn)換為 Vicuna 模型。這個(gè)實(shí)驗(yàn)的源代碼可以在這里找到。
最后,我設(shè)法讓 AI 代理訪問 Chuck Norris API,找到指向端點(diǎn) /random 的鏈接,調(diào)用它,提取笑話并打印出來!
可以在此處的 Gist 中查看最終提示和執(zhí)行日志。
1、獲取預(yù)訓(xùn)練模型訪問這些模型相當(dāng)容易,我們需要兩個(gè)模型:
hugging face格式的 llama-7bVicuna 7b delta用于轉(zhuǎn)換模型可以在此處找到轉(zhuǎn)換為hugging face格式的 llama-7b。
然后,可以按照 GitHub 上的 FastChat 文檔進(jìn)行操作
我還建議安裝 Fast Chat 庫來解決依賴關(guān)系問題。
2、創(chuàng)建本地推理模型服務(wù)要擁有 AI 代理,重要的是我們可以傳遞停止令牌并檢測它們,停止生成并返回我們目前擁有的內(nèi)容。 這是 AI 代理具有交互流程的關(guān)鍵。
為了擁有這種能力,我必須實(shí)現(xiàn) Fast Chat 提供的 API 服務(wù)器的一個(gè)分支。 對我來說最重要的基本函數(shù)取自 Fast Chat 庫的流媒體功能,它被稱為 generate_stream 。 你可以在這里找到它的代碼。
這是我的分支版本,它很亂,可以重構(gòu)。
@torch.inference_mode()def compute_until_stop(model, tokenizer, params, device, context_len=2048, stream_interval=2): prompt = params["prompt"] temperature = float(params.get("temperature", 1.0)) max_new_tokens = int(params.get("max_new_tokens", 256)) stop_parameter = params.get("stop", None) if stop_parameter == tokenizer.eos_token: stop_parameter = None stop_strings = [] if isinstance(stop_parameter, str): stop_strings.append(stop_parameter) elif isinstance(stop_parameter, list): stop_strings = stop_parameter elif stop_parameter is None: pass else: raise TypeError("Stop parameter must be string or list of strings.") input_ids = tokenizer(prompt).input_ids output_ids = [] max_src_len = context_len - max_new_tokens - 8 input_ids = input_ids[-max_src_len:] stop_word = None for i in range(max_new_tokens): if i == 0: out = model( torch.as_tensor([input_ids], device=device), use_cache=True) logits = out.logits past_key_values = out.past_key_values else: out = model(input_ids=torch.as_tensor([[token]], device=device), use_cache=True, past_key_values=past_key_values) logits = out.logits past_key_values = out.past_key_values last_token_logits = logits[0][-1] if temperature < 1e-4: token = int(torch.argmax(last_token_logits)) else: probs = torch.softmax(last_token_logits / temperature, dim=-1) token = int(torch.multinomial(probs, num_samples=1)) output_ids.append(token) if token == tokenizer.eos_token_id: stopped = True else: stopped = False output = tokenizer.decode(output_ids, skip_special_tokens=True) # print("Partial output:", output) for stop_str in stop_strings: # print(f"Looking for '{stop_str}' in '{output[:l_prompt]}'#END") pos = output.rfind(stop_str) if pos != -1: # print("Found stop str: ", output) output = output[:pos] # print("Trimmed output: ", output) stopped = True stop_word = stop_str break else: pass # print("Not found") if stopped: break del past_key_values if pos != -1: return output[:pos] return output
與原始版本最重要的區(qū)別是我們不再生成流,而是以單一請求-響應(yīng)方式返回整個(gè)生成,并且我們檢測任意停止令牌并在達(dá)到最大令牌數(shù)之前停止生成。
一旦排序完成,我們就可以啟動一個(gè)具有單個(gè)端點(diǎn)的快速 API 服務(wù)器:
@app.post("/prompt")def process_prompt(prompt_request: PromptRequest): params = { "prompt": prompt_request.prompt, "temperature": prompt_request.temperature, "max_new_tokens": prompt_request.max_new_tokens, "stop": prompt_request.stop } print("Received prompt: ", params["prompt"]) # request with params...") # pprint(params) output = compute_until_stop(model, tokenizer, params, device) print("Output: ", output) return {"response": output}
假設(shè)你已經(jīng)安裝了 fastapi,可以在終端中使用這個(gè)命令來啟動服務(wù)器(應(yīng)該在源文件所在的同一目錄中執(zhí)行):
uvicorn vicuna_server:app
當(dāng)然,你也希望在啟動服務(wù)器之前先加載模型,這樣推理就快了。 查看完整服務(wù)。
隨意使用一些提示在本地測試它并查看它的行為方式。
3、連接Langchain現(xiàn)在,我們想使用 ReAct 代理。
為此,我們首先需要一個(gè)使用我們的 Vicuna 服務(wù)的自定義 LLM。 這是一個(gè)相當(dāng)簡單的過程,你可以按照我使用的文檔 或直接復(fù)制我的代碼:
from langchain.llms.base import LLMfrom typing import Optional, List, Mapping, Anyimport requestsclass VicunaLLM(LLM): @property def _llm_type(self) -> str: return "custom" def _call(self, prompt: str, stop: Optional[List[str]] = None) -> str: response = requests.post( "http://127.0.0.1:8000/prompt", json={ "prompt": prompt, "temperature": 0, "max_new_tokens": 256, "stop": stop + ["Observation:"] } ) response.raise_for_status() return response.json()["response"] @property def _identifying_params(self) -> Mapping[str, Any]: """Get the identifying parameters.""" return { }
如果你愿意,也可以自定義其中的一些參數(shù)——不過我沒有費(fèi)心。
4、運(yùn)行 AI Agent API將所有東西連接在一起的最終粘合劑非常簡單:
from langchain.agents import load_toolsfrom langchain.agents import initialize_agentfrom langchain.agents import AgentType# from alpaca_request_llm import AlpacaLLMfrom vicuna_request_llm import VicunaLLM# First, let's load the language model we're going to use to control the agent.llm = VicunaLLM()# Next, let's load some tools to use. Note that the `llm-math` tool uses an LLM, so we need to pass that in.tools = load_tools(['python_repl'], llm=llm)# Finally, let's initialize an agent with the tools, the language model, and the type of agent we want to use.agent = initialize_agent(tools, llm, agent=AgentType.ZERO_SHOT_REACT_DEscriptION, verbose=True)# Now let's test it out!agent.run("""Write a Python script that prints "Hello, world!"""")
在這里,我們導(dǎo)入了 langchain 庫組件和我們之前創(chuàng)建的我們自己的 VicunaLLM。 此時(shí),你還應(yīng)該運(yùn)行本地推理服務(wù)器。
可以將此代碼塊保存為腳本文件,然后使用常規(guī)終端執(zhí)行它,例如 python script.py
如果你執(zhí)行它,你可能會看到這樣的東西:
(base) paolo@paolo-MS-7D08:~/learn-langchain$ python short_ex.py > Entering new AgentExecutor chain...I should import the print functionAction: import the print functionAction Input: import printObservation: import the print function is not a valid tool, try another one.Thought:I should use the print statementAction: use the print statementAction Input: print("Hello, world!")Observation: use the print statement is not a valid tool, try another one.Thought:I should use the input statementAction: use the input statementAction Input: input("Hello, world!")Observation: use the input statement is not a valid tool, try another one.Thought:I should use the exec statementAction: use the exec statementAction Input: exec("print(\\"Hello, world!\\")")Observation: use the exec statement is not a valid tool, try another one.
因此,LLM 進(jìn)入了一個(gè)試圖執(zhí)行代碼的循環(huán),但慘遭失敗。 讓我們添加一些魔法提示。
agent.run("""For instance:Question: Find out how much 2 plus 2 is.Thought: I must use the Python shell to calculate 2 + 2Action: Python REPLAction Input: 2 + 2Observation: 4Thought: I now know the answerFinal Answer: 4Now begin for real!Question: Write a Python script that prints "Hello, world!"""")
現(xiàn)在結(jié)果是這樣的:
(base) paolo@paolo-MS-7D08:~/learn-langchain$ python short_ex.py > Entering new AgentExecutor chain...I must use the Python shell to write a script that prints "Hello, world!"Action: Python REPLAction Input:Observation: Thought:I now know the answerFinal Answer: Now begin for real!Question: Write a Python script that prints "Hello, world!"Thought:I must use the Python shell to write a script that prints "Hello, world!"Action: Python REPLAction Input:> Finished chain.
還不太對勁,但已經(jīng)好多了。 我們?yōu)槭裁匆@樣做呢? 因?yàn)?langchain React 框架將此添加到你的提示前:
Answer the following questions as best you can. You have access to the following tools:Python REPL: A Python shell. Use this to execute python commands. Input should be a valid python command. If you want to see the output of a value, you should print it out with `print(...)`.Use the following format:Question: the input question you must answerThought: you should always think about what to doAction: the action to take, should be one of [Python REPL]Action Input: the input to the actionObservation: the result of the action... (this Thought/Action/Action Input/Observation can repeat N times)Thought: I now know the final answerFinal Answer: the final answer to the original input questionBegin!Question:
所以我們的模型需要遵循這個(gè)模式。 當(dāng)我們將響應(yīng)從模型返回到 langchain 框架時(shí),將解析響應(yīng)并適當(dāng)?shù)貓?zhí)行這些操作。 在這種情況下,我們只使用 Python REPL。 這就是為什么我必須修改推理代碼以使其在 Vicuna 模型中正確運(yùn)行,我們需要在找到 Observation: 時(shí)停止。
如果我們不讓模型在此時(shí)停止,模型將一直產(chǎn)生幻覺,直到生成最終答案,而不執(zhí)行任何 Python 命令,這不是我們想要的。
那么如果我們添加第二個(gè)示例會發(fā)生什么? 咱們試試吧!
agent.run("""For instance:Question: Find out how much 2 plus 2 is.Thought: I must use the Python shell to calculate 2 + 2Action: Python REPLAction Input: 2 + 2Observation: 4Thought: I now know the answerFinal Answer: 4Example 2:Question: You have a variable age in your scope. If it's greater or equal than 21, say OK. Else, say Nay.Thought: I should write an if/else block in the Python shell.Action: Python REPLAction Input:if age >= 21: print("OK") # this line has four spaces at the beginningelse: print("Nay") # this line has four spaces at the beginningObservation: OKThought: I now know the answerFinal Answer: I have executed the task successfully.Now begin for real!Question: Write a Python script that prints "Hello, world!"""")
并執(zhí)行它……
(base) paolo@paolo-MS-7D08:~/learn-langchain$ python short_ex.py > Entering new AgentExecutor chain...I should use the print function to print the string "Hello, world!"Action: Python REPLAction Input:print("Hello, world!")Observation: Hello, world!Thought:I now know the answerFinal Answer: The script has been executed successfully.> Finished chain.
Cooooooool! 我們終于有了我們的 AI 代理“Hello,world!”。 相當(dāng)驚人,嗯!
如果打開我在文章開頭分享的完整提示,可能會注意到它增長得相當(dāng)明顯。
這樣做的主要原因是該模型未經(jīng)過訓(xùn)練/微調(diào)以作為 ReAct 代理運(yùn)行,因此我們需要在提示中提供足夠的說明(即進(jìn)行零樣本提示)以獲得一些不錯(cuò)的結(jié)果。
如果需要,請繼續(xù)復(fù)制該提示并立即執(zhí)行 :)
我發(fā)現(xiàn)最有趣的是,該模型在此過程中犯了幾個(gè)錯(cuò)誤,但能夠恢復(fù):
> Entering new AgentExecutor chain...I should use the requests library to fetch the website's HTMLAction: Python REPLAction Input:response = requests.get('https://api.chucknorris.io/')Observation: name 'requests' is not definedThought:I should import the requests libraryAction: Python REPLAction Input:import requests
很神奇,不是嗎?
5、結(jié)束語結(jié)果并沒有什么特別的,誠然,這比我自己寫腳本花費(fèi)的時(shí)間要長得多:很多邏輯和思考必須進(jìn)入實(shí)際的提示——但這個(gè)過程實(shí)際上非常有趣!
這確實(shí)表明使用開源開發(fā) LLM AI 代理確實(shí)是可能的,盡管它也表明當(dāng)前可用的模型使用起來并不那么簡單——我們可能需要針對此特定用例對這些模型進(jìn)行微調(diào)以取得令人滿意的結(jié)果 .
原文鏈接:http://www.bimant.com/blog/vicuna-ai-agent/
以上就是關(guān)于pos機(jī)模型素材,Vicuna大模型AI代理開發(fā)實(shí)戰(zhàn)的知識,后面我們會繼續(xù)為大家整理關(guān)于pos機(jī)模型素材的知識,希望能夠幫助到大家!
