为什么选择GraphRag.Net?
GraphRag.Net采用了Semantic Kernel作为基础,让我们能够非常简洁地抽象出会话与向量接口。因此,用户可以非常方便地实现自己定制的解决方案。接下来,我们会通过一个具体的例子,展示如何将本地模型和国产模型集成到GraphRag.Net中。
默认配置方法
首先,我们来看看如何进行默认配置:
// OpenAI配置``builder.Configuration.GetSection("OpenAI").Get<OpenAIOption>();``// 文档切片配置``builder.Configuration.GetSection("TextChunker").Get<TextChunkerOption>();``// 配置数据库连接``builder.Configuration.GetSection("GraphDBConnection").Get<GraphDBConnectionOption>();`` ``// 注意,需要先注入配置文件,然后再注入GraphRag.Net``builder.Services.AddGraphRagNet();
这里,我们将在默认配置中注入OpenAI的配置、文本切片的配置和数据库连接的配置。然后,依次注入这些配置文件和GraphRag.Net的服务。
自定义配置方法
如果需要自定义模型或本地模型,可能需要实现一些额外的服务接口,下面是自定义配置的示例:
var kernelBuild = Kernel.CreateBuilder();``kernelBuild.Services.AddKeyedSingleton<ITextGenerationService>("mock-text", new MockTextCompletion());``kernelBuild.Services.AddKeyedSingleton<IChatCompletionService>("mock-chat", new MockChatCompletion());``kernelBuild.Services.AddSingleton<ITextEmbeddingGenerationService>(new MockTextEmbeddingGeneratorService());``kernelBuild.Services.AddKeyedSingleton("mock-embedding", new MockTextEmbeddingGeneratorService());`` ``builder.Services.AddGraphRagNet(kernelBuild.Build());
在这个自定义配置示例中,我们引入了三个自定义服务接口:ITextGenerationService
、IChatCompletionService
和ITextEmbeddingGenerationService
。
实现自定义服务接口
接下来,我们需要为每个服务接口提供具体的实现。以下是三个接口的具体实现:
实现**IChatCompletionService
**
`public class MockChatCompletion : IChatCompletionService` `{` `private readonly Dictionary<string, object?> _attributes = new();` `private string _chatId;`` `` ` `private static readonly JsonSerializerOptions _jsonSerializerOptions = new()` `{` `NumberHandling = JsonNumberHandling.AllowReadingFromString,` `Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)` `};`` ` `public IReadOnlyDictionary<string, object?> Attributes => _attributes;`` ` `public MockChatCompletion()` `{`` ` `}`` ` `public async Task<IReadOnlyList<ChatMessageContent>> GetChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)` `{` `StringBuilder sb = new();` `string result = $"这是一条Mock数据,便于聊天测试,你的消息是:{chatHistory.LastOrDefault().ToString()}";` `return [new(AuthorRole.Assistant, result.ToString())];` `}`` ` `public async IAsyncEnumerable<StreamingChatMessageContent> GetStreamingChatMessageContentsAsync(ChatHistory chatHistory, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)` `{` `StringBuilder sb = new();` `string result = $"这是一条Mock数据,便于聊天测试,你的消息是:{chatHistory.LastOrDefault().ToString()}";` `foreach (var c in result)` `{` `yield return new StreamingChatMessageContent(AuthorRole.Assistant, c.ToString());` `}` `}` `}`
实现ITextGenerationService
`public class MockTextCompletion : ITextGenerationService, IAIService` `{` `private readonly Dictionary<string, object?> _attributes = new();` `private string _chatId;`` ` `private static readonly JsonSerializerOptions _jsonSerializerOptions = new()` `{` `NumberHandling = JsonNumberHandling.AllowReadingFromString,` `Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)` `};`` ` `public IReadOnlyDictionary<string, object?> Attributes => _attributes;`` ` `public MockTextCompletion()` `{`` ` `}`` ` `public async Task<IReadOnlyList<TextContent>> GetTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)` `{` `StringBuilder sb = new();` `string result = $"这是一条Mock数据,便于聊天测试,你的消息是:{prompt}";` `return [new(result.ToString())];` `}`` ` `public async IAsyncEnumerable<StreamingTextContent> GetStreamingTextContentsAsync(string prompt, PromptExecutionSettings? executionSettings = null, Kernel? kernel = null, CancellationToken cancellationToken = default)` `{` `StringBuilder sb = new();` `string result = $"这是一条Mock数据,便于聊天测试,你的消息是:{prompt}";` `foreach (var c in result)` `{` `var streamingTextContent = new StreamingTextContent(c.ToString(), modelId: "mock");`` ` `yield return streamingTextContent;` `}` `}` `}`
实现ITextEmbeddingGenerationService
`public sealed class MockTextEmbeddingGeneratorService : ITextEmbeddingGenerationService` `{` `private Dictionary<string, object?> AttributesInternal { get; } = [];` `public IReadOnlyDictionary<string, object?> Attributes => this.AttributesInternal;` `public MockTextEmbeddingGeneratorService()` `{`` ` `}` `public async Task<IList<ReadOnlyMemory<float>>> GenerateEmbeddingsAsync(` `IList<string> data,` `Kernel? kernel = null,` `CancellationToken cancellationToken = default)` `{` `IList<ReadOnlyMemory<float>> results = new List<ReadOnlyMemory<float>>();`` ` `float[] array1 = { 1.0f, 2.0f, 3.0f };` `float[] array2 = { 4.0f, 5.0f, 6.0f };` `float[] array3 = { 7.0f, 8.0f, 9.0f };`` ` `// 将数组包装为ReadOnlyMemory<float>并添加到列表中` `results.Add(new ReadOnlyMemory<float>(array1));` `results.Add(new ReadOnlyMemory<float>(array2));` `results.Add(new ReadOnlyMemory<float>(array3));`` ` `return results;` `}`` ` `public void Dispose()` `{`` ` `}` `}`
看到这里,你可能已经发现,集成自定义模型和本地模型非常简单。只需按照上述步骤,实现相应的接口并注入配置,你就可以在GraphRag.Net中使用这些自定义的功能。
如何学习大模型 AI ?
由于新岗位的生产效率,要优于被取代岗位的生产效率,所以实际上整个社会的生产效率是提升的。
但是具体到个人,只能说是:
“最先掌握AI的人,将会比较晚掌握AI的人有竞争优势”。
这句话,放在计算机、互联网、移动互联网的开局时期,都是一样的道理。
我在一线互联网企业工作十余年里,指导过不少同行后辈。帮助很多人得到了学习和成长。
我意识到有很多经验和知识值得分享给大家,也可以通过我们的能力和经验解答大家在人工智能学习中的很多困惑,所以在工作繁忙的情况下还是坚持各种整理和分享。但苦于知识传播途径有限,很多互联网行业朋友无法获得正确的资料得到学习提升,故此将并将重要的AI大模型资料包括AI大模型入门学习思维导图、精品AI大模型学习书籍手册、视频教程、实战学习等录播视频免费分享出来。
第一阶段(10天):初阶应用
该阶段让大家对大模型 AI有一个最前沿的认识,对大模型 AI 的理解超过 95% 的人,可以在相关讨论时发表高级、不跟风、又接地气的见解,别人只会和 AI 聊天,而你能调教 AI,并能用代码将大模型和业务衔接。
- 大模型 AI 能干什么?
- 大模型是怎样获得「智能」的?
- 用好 AI 的核心心法
- 大模型应用业务架构
- 大模型应用技术架构
- 代码示例:向 GPT-3.5 灌入新知识
- 提示工程的意义和核心思想
- Prompt 典型构成
- 指令调优方法论
- 思维链和思维树
- Prompt 攻击和防范
- …
第二阶段(30天):高阶应用
该阶段我们正式进入大模型 AI 进阶实战学习,学会构造私有知识库,扩展 AI 的能力。快速开发一个完整的基于 agent 对话机器人。掌握功能最强的大模型开发框架,抓住最新的技术进展,适合 Python 和 JavaScript 程序员。
- 为什么要做 RAG
- 搭建一个简单的 ChatPDF
- 检索的基础概念
- 什么是向量表示(Embeddings)
- 向量数据库与向量检索
- 基于向量检索的 RAG
- 搭建 RAG 系统的扩展知识
- 混合检索与 RAG-Fusion 简介
- 向量模型本地部署
- …
第三阶段(30天):模型训练
恭喜你,如果学到这里,你基本可以找到一份大模型 AI相关的工作,自己也能训练 GPT 了!通过微调,训练自己的垂直大模型,能独立训练开源多模态大模型,掌握更多技术方案。
到此为止,大概2个月的时间。你已经成为了一名“AI小子”。那么你还想往下探索吗?
- 为什么要做 RAG
- 什么是模型
- 什么是模型训练
- 求解器 & 损失函数简介
- 小实验2:手写一个简单的神经网络并训练它
- 什么是训练/预训练/微调/轻量化微调
- Transformer结构简介
- 轻量化微调
- 实验数据集的构建
- …
第四阶段(20天):商业闭环
对全球大模型从性能、吞吐量、成本等方面有一定的认知,可以在云端和本地等多种环境下部署大模型,找到适合自己的项目/创业方向,做一名被 AI 武装的产品经理。
- 硬件选型
- 带你了解全球大模型
- 使用国产大模型服务
- 搭建 OpenAI 代理
- 热身:基于阿里云 PAI 部署 Stable Diffusion
- 在本地计算机运行大模型
- 大模型的私有化部署
- 基于 vLLM 部署大模型
- 案例:如何优雅地在阿里云私有部署开源大模型
- 部署一套开源 LLM 项目
- 内容安全
- 互联网信息服务算法备案
- …
学习是一个过程,只要学习就会有挑战。天道酬勤,你越努力,就会成为越优秀的自己。
如果你能在15天内完成所有的任务,那你堪称天才。然而,如果你能完成 60-70% 的内容,你就已经开始具备成为一名大模型 AI 的正确特征了。