0%

設定語言模型

選擇不同語言模型

使用參數model便可以選擇不同的語言模型,預設是openai:gpt-3.5-turbo.

範例

1. openai

(請先完成設定 API Key)

1
2
3
4
5
import akasha

akasha.RAG(embeddings="openai:text-embedding-ada-002",
model="openai:gpt-3.5-turbo")
ak(dir_path, prompt, )


2. huggingface

1
2
3
4
5
import akasha

ak = akasha.RAG(embeddings="hf:Alibaba-NLP/gte-multilingual-base",
model="hf:Qwen/Qwen2.5-7B-Instruct")
ak(dir_path, prompt, )


3. llama-cpp

安裝llama-cpp-python可以使用cpu推論.gguf格式的模型,或是安裝akasha時選擇llama-cpp

1
2
pip install llama-cpp-python
#pip install akasha-terminal[llama-cpp]

llama-cpp允許使用quantized模型並執行在cpu上,你可以從huggingface上下載.gguf llama-cpp 模型,如範例,如果你的模型下載到”model/“路徑下,可以使用以下方法加載模型

1
2
3
4
5
import akasha

ak = akasha.RAG(embeddings="hf:Alibaba-NLP/gte-multilingual-base",
model="llama-cpp:model/llama-2-13b-chat.Q5_K_S.gguf")
ak(dir_path, prompt,)


llama-cpp同樣允許使用gpu運算模型,但安裝套件時需要使用cmake安裝,並確認已安裝g++, gcc和nvidia driver & toolkit,詳細請見llama-cpp-python

1
CMAKE_ARGS="-DGGML_CUDA=on" FORCE_CMAKE=1 python -m pip install --upgrade --force-reinstall llama-cpp-python>=0.3.1 --no-cache-dir


4. 遠端api

如果你使用別人的api或者透過支援openAI api框架的部署自己的模型(例如vllm, TGI, litellm…),你可以使用 remote:{your LLM api url} 來加載模型,若須指定模型名稱,使用 remote:{your LLM api url}@{your model name}

若遠端api需要api金鑰,請先完成設定環境變數 REMOTE_API_KEY ,參考設定 API Key

1
2
3
4
import akasha

ak = akasha.RAG(model="remote:http://140.92.60.189:8081@llama-3.2-11B")
ak(dir_path, prompt, )


5. gemini

(請先完成設定 API Key)

1
2
3
4
5
import akasha

ak = akasha.RAG(embeddings="gemini:models/text-embedding-004",
model="gemini:gemini-1.5-flash")
ak(dir_path, prompt, )


6. anthropic

(請先完成設定 API Key)

1
2
3
4
import akasha

ask_tool = akasha.ask(model="anthropic:claude-3-5-sonnet-20241022")
ask_tool( prompt, info=dir_path, )


推薦一些可使用的模型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
openai_model = "openai:gpt-3.5-turbo"  # need environment variable "OPENAI_API_KEY"
gemini_model="gemini:gemini-1.5-flash" # need environment variable "GEMINI_API_KEY"
anthropic_model = "anthropic:claude-3-5-sonnet-20241022" # need environment variable "ANTHROPIC_API_KEY"
huggingface_model = "hf:meta-llama/Llama-2-7b-chat-hf" #need environment variable "HUGGINGFACEHUB_API_TOKEN" to download meta-llama model
qwen_model = "hf:Qwen/Qwen2.5-7B-Instruct"
quantized_ch_llama_model = "hf:FlagAlpha/Llama2-Chinese-13b-Chat-4bit"
taiwan_llama_gptq = "hf:weiren119/Taiwan-LLaMa-v1.0-4bits-GPTQ"
mistral = "hf:Mistral-7B-Instruct-v0.2"
mediatek_Breeze = "hf:MediaTek-Research/Breeze-7B-Instruct-64k-v0.1"
### If you want to use llama-cpp to run model on cpu, you can download gguf version of models
### from https://huggingface.co/TheBloke/Llama-2-7b-Chat-GGUF and the name behind "llama-cpp:"
### from https://huggingface.co/TheBloke/CodeUp-Llama-2-13B-Chat-HF-GGUF
### is the path of the downloaded .gguf file
llama_cpp_model = "llama-cpp:model/llama-2-13b-chat-hf.Q5_K_S.gguf"
llama_cpp_model = "llama-cpp:model/llama-2-7b-chat.Q5_K_S.gguf"
llama_cpp_chinese_alpaca = "llama-cpp:model/chinese-alpaca-2-7b.Q5_K_S.gguf"
llama_cpp_chinese_alpaca = "llama-cpp:model/chinese-alpaca-2-13b.Q5_K_M.gguf"
chatglm_model = "chatglm:THUDM/chatglm2-6b"




自訂語言模型

如果你想使用其他模型,可以建立一個輸入是prompt的函數並回傳語言模型的回答,並將此函數作為model參數

example

我們建立一個test_model函數,並可以將它作為參數輸入進RAG 或 ask物件回答問題

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import akasha

def test_model(prompt:str):

import openai
from langchain.chat_models import ChatOpenAI
openai.api_type = "open_ai"
model = ChatOpenAI(model="gpt-3.5-turbo", temperature = 0)
ret = model.predict(prompt)

return ret

prompt = "工業4.0是什麼?"

cs = akasha.ask(verbose=True, model = test_model)
cs(prompt = prompt)




建立LLM物件

以上使用model參數選擇模型後,便會在Doc_QA物件內建立模型的物件model_obj(LLM)

1
2
3
4
5
6
import akasha

AK = akasha.RAG(model="openai:gpt-3.5-turbo")

print(type(AK.model_obj))



也可以使用輔助函數建立LLM物件

1
2
3
4
5
6
import akasha.helper as ah

model_obj = ah.handle_model("openai:gpt-3.5-turbo",verbose=False,temperature=0.0)

print(type(model_obj))



此LLM物件也可直接傳入高級物件,避免重複宣告

1
2
3
4
5
6
import akasha
import akasha.helper as ah
model_obj = ah.handle_model("openai:gpt-3.5-turbo",verbose=False,temperature=0.0)

AK = akasha.ask(model=model_obj)

直接使用LLM物件

取得模型類別

使用_llm_type()可取得語言模型的類別

1
2
3
4
5
6

import akasha.helper as ah
model_obj = ah.handle_model("gemini:gemini-1.5-flash",verbose=False,temperature=0.0)

print(model_obj._llm_type()) ## "gemini:gemini-1.5-flash"

模型推論

若要使用語言模型進行推論,可以使用函式call_model

1
2
3
4
5
6
7
8
9
10
11
import akasha.helper as ah
import akasha
from akasha.utils.prompts.gen_prompt import format_sys_prompt
system_prompt = "用中文回答"
prompt = "五軸是什麼?"
model_obj = ah.handle_model("openai:gpt-e3.5-turbo", False, 0.0)
input_text = format_sys_prompt(system_prompt, prompt, "gpt")

response = akasha.call_model(model_obj, input_text)




流輸出

若要呼叫語言模型即時回答,可以使用函式call_stream_model

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import akasha.helper as ah
import akasha
from akasha.utils.prompts.gen_prompt import format_sys_prompt

system_prompt = "用中文回答"
prompt = "五軸是什麼?"
model_obj = ah.handle_model("openai:gpt-e3.5-turbo", False, 0.0)
input_text = format_sys_prompt(system_prompt, prompt, "gpt")

streaming = akasha.call_stream_model(model_obj, input_text)

for s in streaming:
print(s)



批量推論

如果你有大量不需要連貫的推理需求,可以使用akasha.helper.call_batch_model 來進行批量推理來提升速度。

1
2
def call_batch_model(model: LLM, prompt: List[str], 
system_prompt: Union[List[str], str] = "") -> List[str]:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import akasha
import akasha.helper as ah
model_obj = ah.handle_model("openai:gpt-3.5-turbo", False, 0.0)
# this prompt ask LLM to response 'yes' or 'no' if the document segment is relevant to the user question or not.
SYSTEM_PROMPT = "response only 'yes' or 'no', 'yes' if the document segment is relevant to the user question, 'no' for not."
documents = ["Doc1...", "Doc2...", "Doc3...", "Doc4..."]
question = "五軸是什麼?"

prompts = ["document: " + doc +"\n\n" + "User Question: "+ question for doc in documents]

response_list = call_batch_model(model_obj, prompt, SYSTEM_PROMPT)

## ["yes", "no", "yes", "yes"]



輸出格式化

如果你想強制使語言模型輸出符合JSON格式的回答,可以使用 call_JSON_formatter,會轉換語言模型的輸出為字典或字典串列。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import akasha.helper as ah
from pydantic import BaseModel


PROMPT3 = """
openai_model = "openai:gpt-3.5-turbo" # need environment variable "OPENAI_API_KEY"
gemini_model="gemini:gemini-1.5-flash" # need environment variable "GEMINI_API_KEY"
anthropic_model = "anthropic:claude-3-5-sonnet-20241022" # need environment variable "ANTHROPIC_API_KEY"
huggingface_model = "hf:meta-llama/Llama-2-7b-chat-hf" #need environment variable "HUGGINGFACEHUB_API_TOKEN" to download meta-llama model
qwen_model = "hf:Qwen/Qwen2.5-7B-Instruct"
quantized_ch_llama_model = "hf:FlagAlpha/Llama2-Chinese-13b-Chat-4bit"
taiwan_llama_gptq = "hf:weiren119/Taiwan-LLaMa-v1.0-4bits-GPTQ"
mistral = "hf:Mistral-7B-Instruct-v0.2"
mediatek_Breeze = "hf:MediaTek-Research/Breeze-7B-Instruct-64k-v0.1"
"""
model_obj = ah.handle_model("openai:gpt-4o", False, 0.0)


## 使用BaseModel 指定輸出的keys
class Model_Type(BaseModel):
model_type: str
model_name: str


json_response = ah.call_JSON_formatter(model_obj, PROMPT3, keys=Model_Type)
print(json_response)
# response: [{'model_type': 'openai', 'model_name': 'gpt-3.5-turbo'}, {'model_type': 'gemini', 'model_name': 'gemini-1.5-flash'}, {'model_type': 'anthropic', 'model_name': 'claude-3-5-sonnet-20241022'},...