ExileChat
AI知识库 - RAG流程基础功能实现
-
Github地址:github.com/ExileLine/E…
-
基于
FastApi
与tortoise-orm
等异步方式结合openai
实现。
文档规整
- 当前仅支持
.docx
类型,后续补充.pdf
等类型。
# /ExileChat/test/test_ai/test_document_chunk.py
from utils.ai.document_chunk import DocumentChunk
if __name__ == "__main__":
# 文档的路径或静态资源服务器的地址
file_path = "/Users/yangyuexiong/Desktop/ExileChat/test/测试文档.docx"
# 文档中图片存放的路径或图片服务器的地址,`image_base_path` 与 `image_base_url` 使用其一即可
image_base_path = "/Users/yangyuexiong/Desktop/ExileChat/test/test_ai"
image_base_url = "https://www.example.com"
dc = DocumentChunk(image_base_path=image_base_path, is_debug=True)
dc.process_file(file_path)
大模型调用
- 你需要查阅对应大模型的文档以及准备
api_key
,按照例子来使用它。 - 当前仅支持
OpenAI
,AzureOpenAI
,Moonshot
,当然你可以自行添加其他模型,你需要根据以下代码示例实现对应的初始化工作。
# /ExileChat/utils/ai/llm_engine.py
# 代码片段
class ModelClient:
"""Models Client"""
api_key: str = None
kwargs: dict = {}
@classmethod
def open_ai(cls):
"""OpenAI Client"""
...
@classmethod
def azure_open_ai(cls):
"""AzureOpenAI Client"""
...
@classmethod
def moonshot(cls):
"""Moonshot Client"""
...
# 代码片段
class LLMEngine:
"""Large Language Models Engine"""
def __init__(self, model_name: str = None, api_key: str = None, is_debug: bool = False, *args, **kwargs):
self.model_name = model_name
self.api_key = api_key
self.is_debug = is_debug
self.args = args
self.kwargs = kwargs
ModelClient.api_key = api_key
ModelClient.kwargs = kwargs
self.client_dict = {
"open_ai": ModelClient.open_ai,
"azure_open_ai": ModelClient.azure_open_ai,
"moonshot": ModelClient.moonshot,
}
...
- 当然也提供了
websocket
与模型对话应答
# /ExileChat/app/api/chat_ws/chat_ws.py
# 代码片段
# 把鉴权删除,替换模型与提示词即可。
@chat_ws_router.websocket("/{token}/{chat_id}")
async def chat(websocket: WebSocket, token: str, chat_id: str, user: dict = Depends(check_user)):
"""对话"""
await websocket.accept()
if not user:
await websocket.close(code=status.WS_1008_POLICY_VIOLATION)
print("鉴权验证失败 ws 关闭...")
raise CustomException(status_code=403, detail="鉴权验证失败...", custom_code=10005)
try:
while True:
data = await websocket.receive_text()
# await websocket.send_text(f"chat_id: {chat_id} 用户: {user} token: {token} 消息: {data}")
# 测试代码
llm_engine = LLMEngine(model_name='azure_open_ai', api_key=api_key)
llm_engine.system_prompt = "你是一名Python专家"
response_generator = llm_engine.chat(input=data)
async for chunk in response_generator:
if isinstance(chunk, str):
await websocket.send_text(chunk)
else:
print(type(chunk), chunk)
await websocket.close()
break
except Exception as e:
print(f"WebSocket连接发生异常: {e}")
await websocket.close()
多轮对话,流式响应
# /ExileChat/test/test_ai/test_llm_engine.py
import asyncio
from utils.ai.llm_engine import LLMEngine
from api_key import api_key
async def main():
"""main"""
new_engine = LLMEngine(model_name='azure_open_ai', api_key=api_key)
new_engine.system_prompt = "你是一名Python专家"
response_generator = new_engine.chat(input="Python是什么时候诞生的")
async for chunk in response_generator:
if isinstance(chunk, str):
print(f"Received: {chunk}")
else:
print(f"Received chunk type: {type(chunk)}, value: {chunk}")
if __name__ == "__main__":
asyncio.run(main())
一次对话,阻塞响应
- 你可以通过修改
stream
改变响应性方式。
# /ExileChat/test/test_ai/test_llm_engine_only.py
import asyncio
from utils.ai.llm_engine import LLMEngine
from api_key import api_key
async def main():
"""main"""
new_engine = LLMEngine(model_name='azure_open_ai', api_key=api_key)
generated_message = await new_engine.chat_only(prompt="你是强大的人工智能", input="你是谁?")
print(generated_message)
if __name__ == "__main__":
asyncio.run(main())
向量化例子
import asyncio
from utils.ai.llm_engine import LLMEngine
from api_key import api_key
async def main():
"""main"""
llm_engine = LLMEngine(model_name='azure_open_ai', api_key=api_key)
question = "1+1等于多少"
answer = "等于2"
question_embedding = await llm_engine.embedding(text=question)
answer_embedding = await llm_engine.embedding(text=answer)
return question_embedding, answer_embedding
if __name__ == "__main__":
asyncio.run(main())
生成向量化数据结构
- 组合使用:结合大模型生成QA段落并且向量化构造知识库数据。
- 你完全可以不需要理会我项目的表结构和业务逻辑,你得到最终向量化的数据结构,结合自身的业务即可。
# /ExileChat/test/test_ai/test_document_vector.py
import asyncio
from api_key import api_key
from utils.ai.llm_engine import LLMEngine
from utils.ai.document_chunk import DocumentChunk
from utils.ai.document_vector import DocumentVector
"""
文档向量
1.接收已经处理好的文档字符串。
通过`from utils.ai.document_chunk import DocumentChunk`文档规整生成文档字符串,当然你可以使用其他更好方式生成它。
2.接收大模型引擎`LLMEngine`实例,例如以下使用了`azure_open_ai`作为模型。
llm_engine = LLMEngine(model_name='azure_open_ai', api_key=api_key, is_debug=is_debug)
3.使用`LLMEngine`结合`prompt`文档生成`chunks`,同时`self.chunks`也会被赋值,如果你需要自定义可以修改它`self.chunks=[...]`
dv = DocumentVector(...)
dv.gen_chunks()
*.得到例如以下结构的数据,自定义时需要按照以下数据结构。
[
{
"index": 1,
"chunk": "段落1"
},
{
"index": 2,
"chunk": "段落2"
}
...
]
4.使用`LLMEngine`结合`prompt`对段落生成`QA`,同时`self.qa`也会被赋值,如果你需要自定义可以修改它`self.qa=[...]`
dv.gen_qa()
*.得到例如以下结构的数据,自定义时需要按照以下数据结构。
[
{
"Q": "什么是 Python 中的协程?",
"A": "Python 中的协程是一种用于实现异步编程的机制,它允许程序在等待操作完成时挂起执行,从而可以执行其他任务。",
"chunks": [
"Python 中的协程是一种用于实现异步编程的机制,它允许程序在等待操作完成时挂起执行,从而可以执行其他任务。"
]
},
{
"Q": "协程在 Python 中的作用是什么?",
"A": "协程是 Python 语言中的一个特性,用于编写更高效和响应更快的代码。",
"chunks": [
"协程是 Python 语言中的一个特性,用于编写更高效和响应更快的代码。"
]
},
...
]
5.生成`QA`向量。
dv.gen_qa_vector()
*.最终会得到一个组装好的对象列表,例如以下结构的数据。
[
{
'able_id': 0,
'document_id': 0,
'answer': '1+1等于2',
'question': '1+1等于几?',
'chunks': ["1+1=2", "..."],
'answer_embedding': [0.02715362422168255, -0.010939446277916431, 0.006153851747512817, -0.009505090303719044, ...],
'question_embedding': [0.02715362422168255, -0.010939446277916431, 0.006153851747512817, -0.009505090303719044, ...],
},
...
]
"""
if __name__ == '__main__':
is_debug = True
# 为何测试问答的精准,我使用了我的毕业论文作为测试,hhh。
# 你可以把下面 document_content = "1+1=2" 注释打开来进行测试。
# file_path = "/Users/yangyuexiong/Desktop/ExileChat/test/基于Python+Vue自动化测试平台的设计与实现.docx"
file_path = "/Users/yangyuexiong/Desktop/ExileChat/test/测试文档分段.docx"
dc = DocumentChunk(image_base_path="/Users/yangyuexiong/Desktop/ExileChat/test/test_ai", is_debug=is_debug)
document_content = dc.process_file(file_path)
# document_content = "1+1=2"
llm_engine = LLMEngine(model_name='azure_open_ai', api_key=api_key, is_debug=is_debug)
dv = DocumentVector(document_content=document_content, llm_engine=llm_engine, is_debug=is_debug)
asyncio.run(dv.gen_chunks())
asyncio.run(dv.gen_qa())
asyncio.run(dv.gen_qa_vector())
向量化应答
- 问题向量化通过数据库检索得到向量距离最相近的答案,结合大模型生成应答。
补充中...
如何学习AI大模型?
我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。
我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。
第一阶段: 从大模型系统设计入手,讲解大模型的主要方法;
第二阶段: 在通过大模型提示词工程从Prompts角度入手更好发挥模型的作用;
第三阶段: 大模型平台应用开发借助阿里云PAI平台构建电商领域虚拟试衣系统;
第四阶段: 大模型知识库应用开发以LangChain框架为例,构建物流行业咨询智能问答系统;
第五阶段: 大模型微调开发借助以大健康、新零售、新媒体领域构建适合当前领域大模型;
第六阶段: 以SD多模态大模型为主,搭建了文生图小程序案例;
第七阶段: 以大模型平台应用与开发为主,通过星火大模型,文心大模型等成熟大模型构建大模型行业应用。
👉学会后的收获:👈
• 基于大模型全栈工程实现(前端、后端、产品经理、设计、数据分析等),通过这门课可获得不同能力;
• 能够利用大模型解决相关实际项目需求: 大数据时代,越来越多的企业和机构需要处理海量数据,利用大模型技术可以更好地处理这些数据,提高数据分析和决策的准确性。因此,掌握大模型应用开发技能,可以让程序员更好地应对实际项目需求;
• 基于大模型和企业数据AI应用开发,实现大模型理论、掌握GPU算力、硬件、LangChain开发框架和项目实战技能, 学会Fine-tuning垂直训练大模型(数据准备、数据蒸馏、大模型部署)一站式掌握;
• 能够完成时下热门大模型垂直领域模型训练能力,提高程序员的编码能力: 大模型应用开发需要掌握机器学习算法、深度学习框架等技术,这些技术的掌握可以提高程序员的编码能力和分析能力,让程序员更加熟练地编写高质量的代码。
1.AI大模型学习路线图
2.100套AI大模型商业化落地方案
3.100集大模型视频教程
4.200本大模型PDF书籍
5.LLM面试题合集
6.AI产品经理资源合集
👉获取方式:
😝有需要的小伙伴,可以保存图片到wx扫描二v码免费领取【保证100%免费】🆓