假如你和我一样在准备24年的春招,在前端全栈外,再准备一些AIGC的内容是非常有必要的。24年是AI红利年,AIGC+各种岗位大厂机会会多些,同意的同学请点赞。也欢迎朋友们加我微信 shunwuyu, 一起交流。
前言
不管是学习还是工作,我们用的多的还是OpanAI。但是,现在的LLMs百花齐放,很多模型在表现上接近,甚至在某些方面比OpenAI还有优势。当我们想要尝试切换其它LLMs时,虽然LangChain
在框架层面提供了一些LLM通用接口问题,但是在本地或服务器运维、费用管理、云方案这块,还是挺麻烦的。最近,接触到一款新的AI工具Neutrino AI
, 似乎解决了这个问题。那就让我们把它更新到我们的AI工具库里吧。正好最近也想玩下Claude
等一些流程大模型。
Neutrino
首先,让我们来到Neutrino AI (neutrinoapp.com),注册并登录一下。

Neutrino提供了model gateway
模型网关和smart model router
模型智能路由两款产品,帮助开发者智能地将提示词或查询路由给合适的大模型。在保证性能的同时,优化LLM的开销。
Pricing

Neutrino 比较有特色的地方是它的收费栏目。它是靠为我们省了多少钱来收费的。AIGC类的产品,谋求LLM开销和产品体验之间的性价比,是24年AI类产品能否活下来的关键。不同的场景或问题,调用不同的大模型,该省的省,该花的花,Neutrino 带我们过家家。
也就是我们的prompt, 调用Neutrino API来完成,它会去根据需要调用相应模型(tokens开销),我们再需要付15% 的水钱
给Neutrino
。Neutrino 防止了我们杀鸡用牛刀,这点水钱
划算。
现在我们就清楚了,使用Neutrino最大的好处是,不用再操心大模型API访问的申请和开源大模型的布署。我们只需要使用Neutrino API,就可以使用大多数知名大模型。
支持的模型
点击
dashboard
页面的create-router
,我们可以看在到Neutrino
支持哪些模型。从下图看,目前主流的大模型都有在。一些新的,比如Google 最新推出的Gemini
,可能还要过段时间。还有一个好处是,通过使用这个服务,我们不需要再去申请各个大模型的api_key,比如claude, GPT4等。
PlayGround
- 写一封邮件
我们可以在这里尝试一下,比如提问: Draft an email to my boss about the following: I'm going to take 1 day off tomorrow as I'm not feeling very well
我们得到了响应,大家也可以看到这里使用的模型是
mistralai/Mixtral-8x7B-Instruct-v0.1
。这家来自巴黎的LLM新贵,性能直逼GPT-3.5,开源免费。很可惜的是, 目前还不支持中文,所以我们用英文编写了上面的prompt
。
- 写代码
我们再向它提出问题:please show me some code to implement FIFO queue in JS。我们可以在下图看在到,编程场景下它使用的是gpt-3.5-turbo
。OpenAI是收费的,回复的底部列出了开销和延迟。

开销是$1.25e-3
Router
了解Neutrinoapp
的工作方式后,让我们返回Dashboard
创建一个大模型路由(点击Create a Router按钮)。我们可以定义一个大模型的集合,Neutrinoapp会智能的根据我们的pormpt选择合适的大模型完成工作。我在这里想玩下Llama, 至少需要选两个,我选了Llama 2 7B Chat 和 Llama 2 70B Chat。

点击Next, 我们还需要选择一个fallback model,用于Neutrinoapp无法判断该用哪个模型的时候,我们用这个fallback model, 这里选择了Llama2 7B Chat。

接着来到AI Provider Configuration, 使用默认的。
最后给router取个名字,并描述一下,智能大模型路由出炉了。
Dashboard 最下面就会列出刚刚创建的路由。

范例
ini
# Once you've created a router, you can use it like this:
from openai import OpenAI
client = OpenAI(
# Set the OpenAI base url to the Neutrino router API endpoint
base_url="https://router.neutrinoapp.com/api/llm-router",
# Set the API key to the Neutrino API key (you can create one in 'Settings')
api_key="<Neutrino-API-key>"
)
response = client.chat.completions.create(
# Instead of a specific model like 'gpt-3.5-turbo', set this to your router ID
model="<your-neutrino-router-id>",
messages = [{"role": "user", "content": "Hello, how are you?"}],
)
print(response.choices[0].message.content)
Neutrino实现了和OpenAI一样的schema,实例化client的时候,将base_url设置为Neutrino的api地址,就可以让我们像使用OpenAI一样来使用Neutrino,就没啥学习成本。api_key等下我们会去后台拿,这跟我们的账号是绑定的。还有就是model不用再具体指定为gpt-3.5-turbo, 这里写入上面创建的llama-router
就好。
获取api_key
将鼠标移动到页面右上角,可以看到api keys,我们来到页面获取api key。

基于OpenAI SDK 使用Neutrino
- 安装库
diff
!pip install openai
- 引入openai, 设置环境变量
ini
from openai import OpenAI
NEUTRINO_BASE_URL = "https://router.neutrinoapp.com/api/llm-router"
NEUTRINO_API_KEY = "sk-f461cf1c141e49bd6e1259d8ae912324"
NEUTRINO_ROUTER_ID = "llama2-router"
- 实例化client
ini
client = OpenAI(
base_rul=NEUTRINO_ROUTER_ID,
api_key=NEUTRINO_API_KEY
)
response = client.chat.completions.create(
model=NEUTRINO_ROUTER_ID,
messages=[{"role":"user", "content": "Design a slogan for a milky coffee product."}]
)
print(response.choice[0].message.content)

我们打印response,里面就会包含更丰富信息。从下图我们可以看到它使用的是meta-llama/Llama-2-7b-chat...
模型,是Facebook开源的免费模型。

基于llamaIndex 使用Neutrino
- 安装环境
diff
!pip install llamaIndex
- 引入库
javascript
from llama_index.llms import Neutrino
from llama_index.llms import ChatMessage
- 配置环境及实例化
ini
import os
os.environ["NEUTRINO_API_KEY"] = NEUTRINO_API_KEY
llm = Neutrino(
api_key=NEUTRINO_API_KEY,
route=NEUTRINO_ROUTER_ID
)
response = llm.complete("Design a slogan for a milky coffee product.")
print(f"Optimal model: {response.raw['model']}")
print(response)

同样,我们也拿到了结果。想必很多朋友都和我一样,感觉有点奇怪,为什么我们还要用到OpenAI/LlamaIndex?怎么在llamaIndex的例子里,没有去设置base_rul?
OpenAI和LlamaIndex都是开发框架,它们都有一套自己的LLM工作流和api schema。我们可以根据自己熟悉的框架,配置base_url,给Neutrino提供一个语法代理,有效的降低了学习成本。llamaIndex之所以不用配置base_url, 是因为它提供的Neutrino类是一个包装类,它已经将base_url 封装到了它的内部。
总结
- 当我们想要体验或使用多个大模型的时候,Neutrino准备好了。没什么学习成本,智能大模型路由,聪明的帮我们省钱。
- 省去了大模型的布署,对软硬件的要求,不用折腾了。