Llama-Index保存中文文本
def persist(
self, persist_path: str, fs: Optional[fsspec.AbstractFileSystem] = None
) -> None:
"""Persist the store."""
fs = fs or fsspec.filesystem("file")
dirpath = os.path.dirname(persist_path)
if not fs.exists(dirpath):
fs.makedirs(dirpath)
# 保存为英文
# with fs.open(persist_path, "w") as f:
# f.write(json.dumps(self._data))
# 保存为中文
with fs.open(persist_path, "w", encoding="utf-8") as f:
f.write(json.dumps(self._data, ensure_ascii=False))
Llama-Index加载azure embedding模型
# embedding model init
from llama_index.embeddings import OpenAIEmbedding
embed_model = OpenAIEmbedding(
embed_batch_size=1,
deployment_name="text-embedding-ada-002",
api_type='azure',
api_base='https://xxxxxxxxxxxxxxxx.openai.azure.com',
api_key='xxxxxxxxxxxxxxxxxxxx')
ret = embed_model.get_query_embedding("hello")
print(ret)
openai.error.InvalidRequestError: Unrecognized request argument supplied: functions
# 使用chat_engine出错
chat_engine = index.as_chat_engine()
response = chat_engine.chat("这篇文档讲了什么?请用中文回复")
print(response)
# bug由openai各模型版本接口不同引起,选择正确的模型版本即可
from llama_index.llms import AzureOpenAI
llm = AzureOpenAI(
model="gpt-35-turbo-16k",
deployment="gpt-35-turbo-16k",
temperature=0.0,
api_type='azure',
api_base='https://xxxxxxxxxxxxxxxxxxxx.openai.azure.com',
api_key='xxxxxxxxxxxxxxxxxxxxxxxxxxx',
# api_version="2023-05-15", # 错误版本
api_version="2023-07-01-preview", # 正确版本
)
# 库版本
openai 0.28.1
llama-index 0.8.41
[llama-hub] requests.exceptions.SSLError: HTTPSConnectionPool(host='raw.githubusercontent.com', port=443): Max retries exceeded with url: /emptycrown/llama-hub/main/llama_hub/file/docx/requirements.txt (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1122)')))
方案1:替换无效链接
# 无效链接
# LLAMA_HUB_CONTENTS_URL = "https://raw.githubusercontent.com/emptycrown/llama-hub/main"
# 最新链接
LLAMA_HUB_CONTENTS_URL = "https://raw.githubusercontent.com/run-llama/llama-hub/tree/main"
LOADER_HUB_PATH = "/llama_hub"
LOADER_HUB_URL = LLAMA_HUB_CONTENTS_URL + LOADER_HUB_PATH
方案2:手动下载所需文件
下载文件夹:https://github.com/run-llama/llama-hub/tree/main/llama_hub/file
拷贝到:D:\Miniconda3\envs\pynlp\Lib\site-packages\llama_index\readers\llamahub_modules\file