Eden AI
Eden AI 通过整合最佳的 AI 提供商,正在彻底改变 AI 格局,使用户能够释放无限的可能性并挖掘人工智能的真正潜力。它采用一站式综合且无忧的平台,使用户能够以闪电般的速度将 AI 功能部署到生产环境中,从而通过单个 API 轻松访问所有 AI 功能。(网站:https://edenai.co/)
此示例介绍如何使用 LangChain 与 Eden AI 模型进行交互
访问 EDENAI 的 API 需要 API 密钥,
您可以通过创建一个帐户来获取密钥 https://app.edenai.run/user/register 并前往此处 https://app.edenai.run/admin/account/settings
获得密钥后,我们将需要通过运行以下命令将其设置为环境变量
export EDENAI_API_KEY="..."
如果您不想设置环境变量,可以直接通过在初始化 EdenAI LLM 类时使用 edenai_api_key 命名参数直接传递密钥
在初始化 EdenAI LLM 类时
from langchain_community.llms import EdenAI
API 参考:EdenAI
llm = EdenAI(edenai_api_key="...", provider="openai", temperature=0.2, max_tokens=250)
调用模型
EdenAI API 汇集了各种提供商,每个提供商都提供多个模型。
要访问特定模型,您只需在实例化期间添加“model”即可。
例如,让我们探索 OpenAI 提供的模型,例如 GPT3.5
文本生成
from langchain.chains import LLMChain
from langchain_core.prompts import PromptTemplate
llm = EdenAI(
feature="text",
provider="openai",
model="gpt-3.5-turbo-instruct",
temperature=0.2,
max_tokens=250,
)
prompt = """
User: Answer the following yes/no question by reasoning step by step. Can a dog drive a car?
Assistant:
"""
llm(prompt)
API 参考:LLMChain | PromptTemplate
图像生成
import base64
from io import BytesIO
from PIL import Image
def print_base64_image(base64_string):
# Decode the base64 string into binary data
decoded_data = base64.b64decode(base64_string)
# Create an in-memory stream to read the binary data
image_stream = BytesIO(decoded_data)
# Open the image using PIL
image = Image.open(image_stream)
# Display the image
image.show()
text2image = EdenAI(feature="image", provider="openai", resolution="512x512")
image_output = text2image("A cat riding a motorcycle by Picasso")
print_base64_image(image_output)
带有回调的文本生成
from langchain_community.llms import EdenAI
from langchain_core.callbacks import StreamingStdOutCallbackHandler
llm = EdenAI(
callbacks=[StreamingStdOutCallbackHandler()],
feature="text",
provider="openai",
temperature=0.2,
max_tokens=250,
)
prompt = """
User: Answer the following yes/no question by reasoning step by step. Can a dog drive a car?
Assistant:
"""
print(llm.invoke(prompt))
API 参考:EdenAI | StreamingStdOutCallbackHandler
链接调用
from langchain.chains import LLMChain, SimpleSequentialChain
from langchain_core.prompts import PromptTemplate
llm = EdenAI(feature="text", provider="openai", temperature=0.2, max_tokens=250)
text2image = EdenAI(feature="image", provider="openai", resolution="512x512")
prompt = PromptTemplate(
input_variables=["product"],
template="What is a good name for a company that makes {product}?",
)
chain = LLMChain(llm=llm, prompt=prompt)
second_prompt = PromptTemplate(
input_variables=["company_name"],
template="Write a description of a logo for this company: {company_name}, the logo should not contain text at all ",
)
chain_two = LLMChain(llm=llm, prompt=second_prompt)
third_prompt = PromptTemplate(
input_variables=["company_logo_description"],
template="{company_logo_description}",
)
chain_three = LLMChain(llm=text2image, prompt=third_prompt)
# Run the chain specifying only the input variable for the first chain.
overall_chain = SimpleSequentialChain(
chains=[chain, chain_two, chain_three], verbose=True
)
output = overall_chain.run("hats")
# print the image
print_base64_image(output)