跳转到主要内容

NotDiamond Python库,便于轻松集成OpenAI。

项目描述

开始使用Not Diamond

Not Diamond帮助您动态利用多个LLM,实时向最适合的模型发送查询。

主要功能

  • 不是代理:当我们收到查询时,我们的后端服务会返回应调用的模型推荐,实际调用在客户端进行。 因此我们从未看到您的响应输出。
  • 保护隐私:为了保护您的隐私,我们使用模糊哈希来隐藏您的查询字符串,我们的模型推荐引擎完全使用模糊哈希查询。 这意味着我们也从未看到您发送给LLM的原始查询字符串。
  • 最大化性能:通过动态路由到最适合每个查询的LLM,我们提高您的整体LLM输出质量,并提升产品质量。我们的推荐基于数百万个来自严格评估基准和真实世界数据的真实数据点。
  • 降低成本和延迟:大多数情况下,一个小型且专业的模型可以最优地处理给定的查询。我们可以实时帮助您确定这一点,同时降低LLM推理成本并减少延迟。

👍 免费使用!

Not Diamond免费,每月请求量最高100K。超过这个量,只需支付每10K请求10美元。

安装

需要 Python 3.9+

pip install notdiamond

API密钥

注册 并获取Not Diamond API密钥。

创建一个包含您的Not Diamond API密钥以及您希望路由之间的模型的API密钥的 .env 文件。

OPENAI_API_KEY="YOUR_OPENAI_API_KEY"
GOOGLE_API_KEY="YOUR_GOOGLE_API_KEY"
ANTHROPIC_API_KEY="YOUR_ANTHROPIC_API_KEY"
NOTDIAMOND_API_KEY="YOUR_NOTDIAMOND_API_KEY"

或者,您也可以按照以下方式程序化地设置API密钥 如进一步描述

📘 API密钥

notdiamond库在客户端使用您的API密钥调用我们推荐的LLM。 我们绝不会将您的密钥传递到我们的服务器。

🚧 模型访问

由于notdiamond使用您的密钥在客户端调用LLM,因此我们只会调用您有权访问的模型。您还可以使用我们的路由器来确定最佳调用模型,无论您是否有权访问(请参阅示例)。我们的路由器支持大多数流行的开源和专有模型(请参阅完整列表)。

给我留言 如果您有特定的模型需求,我们很乐意与您合作以支持它。

示例

如果您已经在OpenAI SDK或LangChain中拥有现有项目,请查看我们的OpenAILangchain 集成指南。否则,请继续阅读。

在您之前创建的.env文件所在的文件夹中创建一个main.py文件,或者 在Colab中尝试

from notdiamond.llms.llm import NDLLM
from notdiamond.prompts.prompt import NDPrompt, NDContext, NDQuery, NDPromptTemplate


# Define your prompt and query
prompt = NDPrompt("You are a world class software developer.") # The system prompt, defines the LLM's role
query = NDQuery("Write a merge sort in Python.") # The specific query written by an end-user

# Define the prompt template to combine prompt and query into a single string
prompt_template = NDPromptTemplate("About you: {prompt}\n{query}",
                                   partial_variables={"prompt": prompt, "query": query})

# Define the available LLMs you'd like to route between
llm_providers = ['openai/gpt-3.5-turbo', 'openai/gpt-4','openai/gpt-4-1106-preview', 'openai/gpt-4-turbo-preview',
                 'anthropic/claude-2.1', 'anthropic/claude-3-sonnet-20240229', 'anthropic/claude-3-opus-20240229',
                 'google/gemini-pro']

# Create the NDLLM object -> like a 'meta-LLM' combining all of the specified models
nd_llm = NDLLM(llm_providers=llm_providers)

# After fuzzy hashing the inputs, the best LLM is determined by the ND API and the LLM is called client-side
result, session_id, provider = nd_llm.invoke(prompt_template=prompt_template)


print("ND session ID: ", session_id)  # A unique ID of the invoke. Important for future references back to ND API
print("LLM called: ", provider.model)  # The LLM routed to
print("LLM output: ", result.content)  # The LLM response

👍 运行它!

python main.py

项目详情


下载文件

下载适用于您的平台的文件。如果您不确定要选择哪个,请了解更多关于安装包的信息。

源分发

notdiamond_openai-0.1.4.tar.gz (145.5 kB 查看哈希)

上传时间

构建分发

notdiamond_openai-0.1.4-py3-none-any.whl (265.4 kB 查看哈希)

上传时间 Python 3

由以下支持

AWS AWS 云计算和安全赞助商 Datadog Datadog 监控 Fastly Fastly CDN Google Google 下载分析 Microsoft Microsoft PSF 赞助商 Pingdom Pingdom 监控 Sentry Sentry 错误记录 StatusPage StatusPage 状态页