快速使用指南:Ollama與CodeGeeX4-ALL-9B的集成教程

2025-01-03 11:39 更新

快速使用

Ollama教程

Ollama也是一個開源項目,是在本地快速跑各種開源大模型的最優(yōu)選擇之一。CodeGeeX4-ALL-9B在開源后不到24小時就獲得了Ollama的支持,目前通過Ollama下載已經超過了10,000次+。

Mac環(huán)境下的配置教程:

安裝使用的過程非常簡單,跟著下面的教程,大家也可以一起來體驗:

首先,把Ollama開源項目一鍵安裝在自己的電腦上。建議安裝Ollama0.2或更高版本。MacOS和Windows都有一鍵安裝包。Linux也只需要執(zhí)行一行命令。

安裝完成后,打開終端,輸入ollama,能看到這些信息就說明Ollama已經安裝成功。

接下來我們打開Ollama的官網,搜索CodeGeeX4。

打開進入詳情頁面,就可以看到CodeGeeX4-ALL-9B模型的相關介紹和使用命令了,復制運行命令

在終端運行剛才復制的模型運行命令,就開始安裝CodeGeeX4-ALL-9B模型了

看到終端命令行提示的“Send a message”就說明CodeGeeX4-ALL-9B已經成功安裝在你的電腦上,你可以直接在這里輸入問題和CodeGeeX4-ALL-9B進行對話。

接下來按照下面的步驟,就可以把CodeGeeX4-ALL-9B接入到您的CodeGeeX插件本地模式中。

兩個平臺的步驟相同。

  1. VSCodeJetBrains全家桶的IDE插件市場中,搜索CodeGeeX,點擊下載安裝插件

  1. 配置跨域所需的環(huán)境變量在終端輸入

export OLLAMA_ORIGINS="*"

launchctl setenv OLLAMA_ORIGINS "*"

來設置環(huán)境變量,Windows環(huán)境可以觀看下方視頻,了解如何手動配置環(huán)境變量。

  1. 設置后需要重啟 Ollama 服務和 IDE(VSCode 或其他環(huán)境) 使環(huán)境變量生效。

  1. 啟動CodeGeeX4,在終端輸入

ollama serve

打開一個新的終端,在終端輸入

ollama run codegeex4
  1. 配置接口地址在CodeGeeX插件的本地模式設置中,輸入模型地址:

http://localhost:11434/v1/chat/completions

打開模型配置的高級模式,在模型名稱欄填寫

codegeex4

現在就可以享受 CodeGeeX4在本地提供的編碼體驗!

希望了解更多模型部署的教程,可以前往CodeGeeX4在Github上的教程與Demo查看,如果您喜歡我們的項目并認為它對您有幫助,請在GitHub上為CodeGeeX4點一個?? Star!

Huggingface Transformers

請使用 4.39.0<=transformers<=4.40.2 部署 codegeex4-all-9b

from transformers import AutoTokenizer, AutoModelForCausalLM

device = "cuda" if torch.cuda.is_available() else "cpu"
tokenizer = AutoTokenizer.from_pretrained("THUDM/codegeex4-all-9b", trust_remote_code=True)
model = AutoModelForCausalLM.from_pretrained(
"THUDM/codegeex4-all-9b",
torch_dtype=torch.bfloat16,
low_cpu_mem_usage=True,
trust_remote_code=True
).to(device).eval()
inputs = tokenizer.apply_chat_template([{"role": "user", "content": "write a quick sort"}], add_generation_prompt=True, tokenize=True, return_tensors="pt", return_dict=True).to(device)
with torch.no_grad():
outputs = model.generate(**inputs)
outputs = outputs[:, inputs['input_ids'].shape[1]:]
print(tokenizer.decode(outputs[0], skip_special_tokens=True))

vLLM

使用 vllm==0.5.1 快速啟動 codegeex4-all-9b

from transformers import AutoTokenizer
from vllm import LLM, SamplingParams

# CodeGeeX4-ALL-9B
# max_model_len, tp_size = 1048576, 4
# If OOM,please reduce max_model_len,or increase tp_size
max_model_len, tp_size = 131072, 1
model_name = "codegeex4-all-9b"
prompt = [{"role": "user", "content": "Hello"}]

tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
llm = LLM(
model=model_name,
tensor_parallel_size=tp_size,
max_model_len=max_model_len,
trust_remote_code=True,
enforce_eager=True,
# If OOM,try using follong parameters
# enable_chunked_prefill=True,
# max_num_batched_tokens=8192
)
stop_token_ids = [151329, 151336, 151338]
sampling_params = SamplingParams(temperature=0.95, max_tokens=1024, stop_token_ids=stop_token_ids)

inputs = tokenizer.apply_chat_template(prompt, tokenize=False, add_generation_prompt=True)
outputs = llm.generate(prompts=inputs, sampling_params=sampling_params)

print(outputs[0].outputs[0].text)

通過 vllm 設置 OpenAI 兼容服務,詳細信息請查看 OpenAI 兼容服務器

python -m vllm.entrypoints.openai.api_server \
--model THUDM/codegeex4-all-9b \
--trust_remote_code


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號