哈尼哈尼尼 發表於 2025-3-5 08:45:00

Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库

<style>\3c span id="mce_marker" data-mce-type="bookmark">\3c /span>\3c span id="__caret">_\3c /span>\3c !--
#div_digg { float: right; font-size: 12px; margin: 10px; text-align: center; width: 120px; position: fixed; right: 0; bottom: 0; z-index: 10; background-color: rgba(255, 255, 255, 1); padding: 10px; border: 1px solid rgba(204, 204, 204, 1) }
#cnblogs_post_body pre code span { font-family: Consolas, monospace }
#blogTitle>h2 { font-family: Consolas, monospace }
#blog-news { font-family: Consolas, monospace }
#topics .postTitle a { font-family: Georgia, Times New Roman, Times, sans-serif, monospace; font-weight: bold }
#cnblogs_post_body p { margin: 18px auto; color: rgba(0, 0, 0, 1); font-family: Georgia, Times New Roman, Times, sans-serif, monospace; font-size: 16px; text-indent: 0 }
#cnblogs_post_body h1 { font-family: Georgia, Times New Roman, Times, sans-serif, monospace; font-size: 32px; font-weight: bold; line-height: 1.5; margin: 10px 0 }
#cnblogs_post_body h2 { font-family: Consolas, "Microsoft YaHei", monospace; font-size: 26px; font-weight: bold; line-height: 1.5; margin: 20px 0 }
#cnblogs_post_body h3 { font-family: Georgia, Times New Roman, Times, sans-serif, monospace; font-size: 20px; font-weight: bold; line-height: 1.5; margin: 10px 0 }
#cnblogs_post_body h4 { font-family: Georgia, Times New Roman, Times, sans-serif, monospace; font-size: 18px; font-weight: bold; margin: 10px 0 }
em { font-style: normal; color: rgba(0, 0, 0, 1) }
#cnblogs_post_body ul li { font-family: Georgia, Times New Roman, Times, sans-serif, monospace; color: rgba(0, 0, 0, 1); font-size: 16px; list-style-type: disc }
#cnblogs_post_body ol li { font-family: Georgia, Times New Roman, Times, sans-serif, monospace; color: rgba(0, 0, 0, 1); font-size: 16px; list-style-type: decimal }
#cnblogs_post_body a:link { text-decoration: none; color: rgba(0, 44, 153, 1) }
#topics .postBody blockquote { background: rgba(255, 243, 212, 1); border-top: none; border-right: none; border-bottom: none; border-left: 5px solid rgba(246, 183, 60, 1); margin: 0; padding-left: 10px }
.cnblogs-markdown code { font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.8; background-color: rgba(245, 245, 245, 1) !important; border: none !important; padding: 0 5px !important; border-radius: 3px !important; margin: 1px 5px; vertical-align: middle; display: inline-block }
.cnblogs-markdown .hljs { font-family: Consolas, "Microsoft YaHei", monospace !important; font-size: 16px !important; line-height: 1.5 !important; padding: 5px !important }
#cnblogs_post_body h1 code, #cnblogs_post_body h2 code { font-size: inherit !important; border: none !important }</style>
<h1>Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库</h1>
<h2>前言</h2>
<p>在 AI 飞速发展的时代,大型语言模型(LLMs)逐渐成为推动技术进步的重要力量。无论是自然语言处理、文本生成,还是智能问答和代码辅助,LLMs 的应用场景正在不断扩展,各种模型层出不穷。然而,面对种类繁多的模型和各自不同的接口标准,开发者在集成和管理这些模型时往往会面临复杂性和兼容性的问题。</p>
<p><code>Pantheons</code> 的诞生正是为了解决这一痛点。它是一个使用 TypeScript 基于 OpenAI Node.js SDK 构建的统一对话库,旨在为开发者提供一个简洁、高效的接口,方便与多个大型语言模型(LLMs)进行交互。通过 Pantheons,开发者可以轻松集成 OpenAI、DeepSeek、DashScope、Gemini 等主流语言模型,无需担心底层差异,专注于实现自己的业务逻辑。</p>
<h2>功能特性</h2>
<ul>
<li>统一接口设计:所有模型基于 OpenAI Node.js SDK 构建;共享相同的调用方式,大幅降低学习成本</li>
<li>类型安全:基于 TypeScript 构建,提供完整的类型定义,让开发更加顺畅</li>
<li>支持多种模型:目前支持十几种主流大语言模型,包括 OpenAI、Azure OpenAI、通义千问、文心一言、腾讯混元、Google Gemini 等,覆盖几乎所有主流云端和本地 LLM 服务。</li>
<li>适配多种运行环境:支持 Node.js, Bun 和 Web 等多种运行时环境,适配性强。</li>
</ul>
<h2>支持的大模型</h2>
<ul>
<li>OpenAI</li>
<li>Azure OpenAI</li>
<li>通义千问(DashScope)</li>
<li>腾讯混元(HunYuan)</li>
<li>月之暗面(Moonshot)</li>
<li>硅基流动(SiliconFlow)</li>
<li>DeepSeek</li>
<li>文心一言(QianFan)</li>
<li>Gemini</li>
<li>Ollama</li>
<li>智谱清言(ZhiPu)</li>
<li>XAI</li>
<li>零一万物(LingYiWanWu)</li>
<li>MiniMax</li>
<li>讯飞星火(Spark)</li>
<li>Anthropic(Claude)</li>
</ul>
<h2>使用方法</h2>
<h4>Nodejs</h4>
<pre><code>
import { DeepSeek } from 'pantheons';

(async () =&gt; {
const client = new DeepSeek('Your key');
const stream = await client.stream({
    model: 'deepseek-chat',
    stream: true,
    messages: [{ role: 'user', content: 'Hi!' }],
});

let result = '';
for await (const chunk of stream) {
    result += chunk.choices.delta?.content;
}

console.log(result);
})();
</code>
</pre>
<h4>Bun</h4>
<pre><code>
import { DeepSeek } from '@greywen/pantheons';

const client = new DeepSeek('Your key');
const stream = await client.stream({
model: 'deepseek-chat',
stream: true,
messages: [{ role: 'user', content: 'Hi!' }],
});

let result = '';
for await (const chunk of stream) {
result += chunk.choices.delta?.content;
}

console.log(result);
</code>
</pre>
<h4>多模型</h4>
<pre><code>
import { DashScope, Moonshot, DeepSeek } from 'pantheons';

const deepSeekClient = new DeepSeek('Your key');
const dashScopeClient = new DashScope('Your key');
const moonshotClient = new Moonshot('Your key');

const messages = [{ role: 'user', content: 'Hi!' }];

const deepSeekStream = await dashScopeClient.stream({
model: 'deepseek-chat',
stream: true,
messages,
});

const dashScopeStream = await dashScopeClient.stream({
model: 'qwen-max',
stream: true,
messages,
});

const moonshotStream = await moonshotClient.stream({
model: 'kimi-latest',
stream: true,
messages,
});

async function readStream(stream: AsyncIterable&lt;any&gt;, output: string[]) {
for await (const chunk of stream) {
    const content = chunk.choices.delta?.content || '';
    output.push(content);
}
}

const deepSeekOutput: string = [];
const dashScopeOutput: string[] = [];
const moonshotOutput: string[] = [];

await Promise.all([
readStream(deepSeekStream, deepSeekOutput),
readStream(dashScopeStream, dashScopeOutput),
readStream(moonshotStream, moonshotOutput),
]);

console.log(deepSeekOutput, dashScopeOutput, moonshotOutput);
</code>
</pre>
<h4>私有化部署URL</h4>
<pre><code>
import { Ollama } from 'pantheons';

client = new Ollama('Your Key', { baseURL: 'Your URL' });

const stream = await client.stream({
    model: 'qwen2.5-coder:latest',
    stream: true,
    messages: [{ role: 'user', content: 'Hi!' }],
});

let actual = '';
for await (const chunk of stream) {
   actual += chunk.choices.delta?.content;
}

console.log(actual);
</code>
</pre>
<h2>总结</h2>
<p>Pantheons 是一个面向多模型集成工具,借助其统一、高效的接口设计,开发者可以显著减少在多语言模型集成中的开发成本和时间。无论你是希望快速接入一个模型,还是需要在多个模型之间自由切换,Pantheons 都能成为你不可或缺的工具。</p>
<p>未来,Pantheons 将继续扩展更多模型的支持,同时优化性能与易用性,为开发者提供更强大的工具链。如果你正在寻找一个解决多模型集成痛点的方案,不妨试试 Pantheons。</p>
<p>欢迎广大开发者 Star 、提交 issue、贡献代码、参与讨论, 感谢!</p>
<p>同时也欢迎大家使用我们已发布的大模型项目 Sdcb Chats 如果您觉得有帮助请在 GitHub 上 Star 我们!您的支持是我们前进的动力。</p>
<p>再次感谢您的支持,期待未来为您带来更多惊喜!</p><br><br>
来源:https://www.cnblogs.com/greywen/p/18747300
頁: [1]
查看完整版本: Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库