广州公司网站设计,一块钱涨1000粉网站,wordpress文章页面没有格式调整,有专门做辩论的网站吗在基于数据构建任何 LLM 应用程序时#xff0c;选择合适的大型语言模型 #xff08;LLM#xff09; 是您需要考虑的首要步骤之一。
LLM 是 LlamaIndex 的核心组成部分。它们可以作为独立模块使用#xff0c;也可以插入到其他核心 LlamaIndex 模块#xff08;索引、检索器…在基于数据构建任何 LLM 应用程序时选择合适的大型语言模型 LLM 是您需要考虑的首要步骤之一。
LLM 是 LlamaIndex 的核心组成部分。它们可以作为独立模块使用也可以插入到其他核心 LlamaIndex 模块索引、检索器、查询引擎中。
LlamaIndex 提供了一个统一的接口来定义 LLM 模块支持对接世面上多种LLM大模型能力。
支持文本完成和聊天端点支持流式处理和非流式处理终结点支持同步和异步端点 本Llamaindex系列文章使用的模型是阿里的灵积平台 Llamaindex 对接 QWen LLM
在Llamaindex中使用LLM很简单在这里你可以看到目前Llamaindex已经支持的LLM类型下面我将使用QWen模型实现一个入门案例。
安装相关依赖
# 引入灵积平台依赖
!pip install llama-index-llms-dashscope --quiet
# 加载环境变量
!pip install python-dotenv --quiet初始化模型Client对象
from llama_index.llms.dashscope import DashScope, DashScopeGenerationModels
from dotenv import load_dotenv# 加载环境变量
load_dotenv()dashscope_llm DashScope(model_nameDashScopeGenerationModels.QWEN_TURBO,max_tokens1000,enable_searchFalse,temperature0.2
)关于如何使用dotenv可以参考文章《揭秘python-dotenv:那些鲜为人知的实用窍门》 构建complete回话
# 同步输出
resp dashscope_llm.complete(你好)
print(resp)构建stream会话
# 流式输出
responses dashscope_llm.stream_complete(你好)
for response in responses:print(response.delta, end)构建Chat会话
# chat 模型会话
from llama_index.core.base.llms.types import MessageRole, ChatMessagemessages [ChatMessage(roleMessageRole.SYSTEM, content你是一个AI智能机器人),ChatMessage(roleMessageRole.USER, content你好。),
]
resp dashscope_llm.chat(messages)
print(resp)构建多轮会话
# 多轮对话
messages [ChatMessage(roleMessageRole.SYSTEM, content你是一个AI智能机器人),ChatMessage(roleMessageRole.USER, content你好。),
]
# first round
resp dashscope_llm.chat(messages)
print(resp)# add response to messages.
messages.append(ChatMessage(roleMessageRole.ASSISTANT, contentresp.message.content)
)messages.append(ChatMessage(roleMessageRole.USER, content如何制作一个蛋糕)
)
# second round
resp dashscope_llm.chat(messages)
print(resp)LLamaindex 设置Prompt
在LLamaindex中使用Prompt就像创建格式字符串一样简单
from llama_index.core import PromptTemplatetemplate (我提供的上下文内容如下 \n---------------------\n{context_str}\n---------------------\n基于给出的内容回答一下问题: {query_str}\n
)
qa_template PromptTemplate(template)context_str
重达3000吨、总长超70米、20层楼高度又一“大国重器”成功问世此“重器”的诞生也标志我国自研冲破西方“壁垒”。
据悉这个形状酷似“茅台“的国器是目前世界上最大的加氢反应器其在石油工业中的地位“媲美芯片”。
不少西方国家对于此项技术给出高价但我们表示100%中国制造永不出售query_str 总结一下加氢反应器
# you can create text prompt (for completion API)
prompt qa_template.format(context_strcontext_str, query_strquery_str)
print(prompt)
# or easily convert to message prompts (for chat API)
messages qa_template.format_messages(context_strcontext_str, query_strquery_str)
print(messages)除此之外你还可以定义Chat格式的Prompt
from llama_index.core import ChatPromptTemplate
from llama_index.core.llms import ChatMessage, MessageRolemessage_templates [ChatMessage(contentYou are an expert system., roleMessageRole.SYSTEM),ChatMessage(contentGenerate a short story about {topic},roleMessageRole.USER,),
]
chat_template ChatPromptTemplate(message_templatesmessage_templates)# you can create message prompts (for chat API)
messages chat_template.format_messages(topic狼来了)
print(messages)
# or easily convert to text prompt (for completion API)
prompt chat_template.format(topic狼来了)
print(prompt)Llamaindex 支持 Embedding
Embedding是一种将离散数据映射到连续空间的表示方法。在自然语言处理中Embedding技术可以将单词、句子等文本数据转化为低维向量从而捕捉文本的语义信息。
在LlamaIndex中嵌入用于将文本数据映射到语义空间使得相似的文本在向量空间中靠近。这种表示方式对于语义搜索、文本分类和信息检索等任务至关重要。通过嵌入LlamaIndex能够理解和处理文本的细微差异从而提供更精准和个性化的服务。
LlamaIndex利用Embedding技术实现文本向量的生成具体步骤如下
1预处理对文本进行清洗、切块等处理。
2构建Embedding模型使用预训练的Embedding模型如Word2Vec、BERT等将文本转化为向量。
3向量存储与搜索与LLM应用类似将向量存储到向量数据库中并进行相似度检索。
目前LLamaindex已经支持了很多Embedding模型你可以在这里查看本次Embedding使用的是灵积平台中的Embedding模型。
文件内容Embedding
# imports
from llama_index.embeddings.dashscope import (DashScopeEmbedding,DashScopeTextEmbeddingModels,DashScopeTextEmbeddingType,
)# Create embeddings
# text_typedocument to build index
embedder DashScopeEmbedding(model_nameDashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2,text_typeDashScopeTextEmbeddingType.TEXT_TYPE_DOCUMENT,
)text_to_embedding [风急天高猿啸哀, 渚清沙白鸟飞回, 无边落木萧萧下, 不尽长江滚滚来]
# Call text Embedding
result_embeddings embedder.get_text_embedding_batch(text_to_embedding)
# requests and embedding result index is correspond to.
for index, embedding in enumerate(result_embeddings):if embedding is None: # if the correspondence request is embedding failed.print(The %s embedding failed. % text_to_embedding[index])else:print(Dimension of embeddings: %s % len(embedding))print(Input: %s, embedding is: %s% (text_to_embedding[index], embedding[:5]))查询Embedding
# imports
from llama_index.embeddings.dashscope import (DashScopeEmbedding,DashScopeTextEmbeddingModels,DashScopeTextEmbeddingType,
)
# Create embeddings
# text_typequery to retrieve relevant context.
embedder DashScopeEmbedding(model_nameDashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2,text_typeDashScopeTextEmbeddingType.TEXT_TYPE_QUERY, #指定对查询问题进行Embedding
)# Call text Embedding
embedding embedder.get_text_embedding(骆驼祥子这本书讲了什么)
print(fDimension of embeddings: {len(embedding)})
print(embedding[:5])后面的章节将会继续说明如何将向量化的内容存储到向量数据库中以及如何对向量化结果进行Retrieval。 最后
本篇文章仅带着大家认识一下LLamaindex的LLM、Prompt以及Embedding相关功能实际上你会发现LLamaindex的能力和Langchain是非常相似的甚至LLamaindex可以和Langchain一起使用。
后面咱们会着重研究一下基于LLamaindex搭建使用RAG增强的ChatBot以及相关的组件能力。