| 步骤 | 积分 | 备注 |
|---|---|---|
| 完成调研问卷+完成 本实验手册 | 5 分 | 每人仅限一次 |
| 完成黑客松 demo | 20 分 | 结合自己的想法,基于 NIM 平台模型做一个简单小实验(例如数据分析、看图写作、代码用例测试),需要将代码发表在 CSDN 博客上,最好配有至少 150 字左右的文档说明。 每人仅限一次,完成送 150 元京东卡,数量有限 |
1.完成调研问卷
建议填写与入群信息一致的手机号码
https://marketing.csdn.net/questions/Q2409141734562510630
2.NIM平台 入门体验 亲手搭建“基于NVIDIA NIM 平台的知识问答系统
-账号注册
-安装Python
-导入相关依赖项
-获取NVIDIA API密钥
-启动项目
访问 ai.nvidia.com ,点击 Try Now

在打开的的页面点击右上角的 Login

输入注册的邮箱

邮箱通过验证后,继续按照操作提示创建 NVIDIA 云账户

创建成功后,将会返回 NIM 平台的主页面,到这里您就用有了 NVIDIA云账户 。
再次点击右上角按钮,可以看到剩余免费额度,点击Request More 可以进行企业用户认证,获取更多使用额度
在这个界面我们能看到多种基于 NIM 平台的AI模型,接下来我们将介绍其中之一的大语言模型,构建我们的知识问答系统。

NIM 平台提供两种使用方法,第一种是 API 调用,第二种是 docker 容器部署的方式。
本次介绍通过 API 的方式使用 NIM 平台的模型。
使用 NIM 的 API 需要安装 Python 环境 ,如果你已经安装了 Python,此步骤可跳过。
如果已经配置过Python环境,可直接跳到第三步导入依赖项
安装Python环境为大家介绍两种安装方法
推荐阅读这篇文章 Pycharm+Python环境安装在本地安装 Python+Pycharm 环境
安装好后,启动 Pycharm ,如果出现 Import Pycharm Settings 页面,勾选Do not import settings,点击OK(没有这个弹框则看后续步骤)
看到 Welcome to Pycharm 页面,就代表首次启动好了。点击 New Project 即可创建新项目
一般只需要改一下项目存放位置(项目名一般使用英文命名),然后点击create
通过安装anaconda 完成Python环境:https://blog.csdn.net/kunhe0512/article/details/140910139
创建好项目后,右键项目文件夹创建一个新的Python文件,名称自拟,最好也是英文,例如“nim_test.py”
然后再次右键点击这个项目-Open in-Terminal 打开终端
在打开的窗口安装我们所需要的依赖项,输入下方命令
pip install langchain_nvidia_ai_endpoints langchain-community langchain-text-splitters faiss-cpu gradio==3.50.0 setuptools beautifulsoup4
根据网络状况,等待安装完成,大约 10-30 分钟
然后我们把下面的代码复制粘贴到一开始创建的 Python 文件中,例如“nim_test.py”
# -*- coding: utf-8 -*-
# 导入必要的库
from langchain_nvidia_ai_endpoints import NVIDIAEmbeddings, ChatNVIDIA
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import FAISS
from langchain_text_splitters import RecursiveCharacterTextSplitter
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import chain
import os
import gradio as gr
from datetime import datetime
# Even if you do not know the full answer, generate a one-paragraph hypothetical answer to the below question in Chinese
# 定义假设性回答模板
hyde_template = """Even if you do not know the full answer, generate a one-paragraph hypothetical answer to the below question:
{question}"""
# 定义最终回答模板
template = """Answer the question based only on the following context:
{context}
Question: {question}
"""
# 定义函数来处理问题
def process_question(url, api_key, model_name, question):
# 初始化加载器并加载数据
loader = WebBaseLoader(url)
docs = loader.load()
# 设置环境变量
os.environ['NVIDIA_API_KEY'] = api_key
# 初始化嵌入层
embeddings = NVIDIAEmbeddings()
text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
documents = text_splitter.split_documents(docs)
vector = FAISS.from_documents(documents, embeddings)
retriever = vector.as_retriever()
# 初始化模型
model = ChatNVIDIA(model=model_name)
# 创建提示模板
hyde_prompt = ChatPromptTemplate.from_template(hyde_template)
hyde_query_transformer = hyde_prompt | model | StrOutputParser()
# 定义检索函数
@chain
def hyde_retriever(question):
hypothetical_document = hyde_query_transformer.invoke({"question": question})
return retriever.invoke(hypothetical_document)
# 定义最终回答链
prompt = ChatPromptTemplate.from_template(template)
answer_chain = prompt | model | StrOutputParser()
@chain
def final_chain(question):
documents = hyde_retriever.invoke(question)
response = ""
for s in answer_chain.stream({"question": question, "context": documents}):
response += s
return response
# 调用最终链获取答案
return str(datetime.now())+final_chain.invoke(question)
# 定义可用的模型列表
models = ["mistralai/mistral-7b-instruct-v0.2","meta/llama-3.1-405b-instruct"]
# 启动Gradio应用
iface = gr.Interface(
fn=process_question,
inputs=[
gr.Textbox(label="输入需要学习的网址"),
gr.Textbox(label="NVIDIA API Key"),
gr.Dropdown(models, label="选择语言模型"),
gr.Textbox(label="输入问题")
],
outputs="text",
title="网页知识问答系统"
)
# 启动Gradio界面
iface.launch()
访问 https://build.nvidia.com/meta/llama-3_1-405b-instruct
点击Python标签下的Get API Key ,在弹出的框中点击 Generate Key
右键单击Python项目文件-点击Run
等待数秒,在下方运行窗口中,点击 URL 地址
在打开的浏览器中填入上面复制的 API 密钥、需要学习的网页地址、使用的语言模型以及提出的问题后,点击submit,等待一段时间 即可看到回答的信息。
完成后将上方运行画面截图以及您入群填写的手机号码 发给群内@小助手