Eden AI
Eden AI是一家AI咨询公司,旨在利用其资源赋能人们,并创建使用AI改善个人、企业和整个社会生活质量的有影响力的产品。
本示例介绍如何使用LangChain与Eden AI模型进行交互。
访问EDENAI的API需要一个API密钥,您可以通过创建一个账户(https://app.edenai.run/user/register)并前往此处(https://app.edenai.run/admin/account/settings)获取。
一旦我们获得了密钥,我们将通过运行以下命令将其设置为环境变量:
export EDENAI_API_KEY="..."
如果您不想设置环境变量,您可以直接通过edenai_api_key命名参数传递密钥,在初始化EdenAI LLM类时使用:
from langchain.llms import EdenAI
API参考:
- EdenAI 来自
langchain.llms
llm = EdenAI(edenai_api_key="...", provider="openai", params={"temperature": 0.2, "max_tokens": 250})
调用模型
EdenAI API汇集了各种提供商,每个提供商都提供多个模型。
要访问特定的模型,您可以在调用时使用相应的"settings"。
例如,让我们探索由OpenAI提供的模型,如GPT3.5
文本生成
from langchain import PromptTemplate, LLMChain
llm = EdenAI(feature="text", provider="openai", params={"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, settings={'openai' : 'text-davinci-003'})
输出结果为:
" No, a dog cannot drive a car.\n\nReasoning: \n\n1. A dog does not have the physical capability to operate a car. \n2. A dog does not have the cognitive ability to understand the rules of the road and the mechanics of driving. \n3. A dog does not have a driver's license, which is a legal requirement to operate a motor vehicle. \n\nTherefore, a dog cannot drive a car."
图像生成
```python
import base64
from io import BytesIO
from PIL import Image
import json
def print_base64_image(base64_string):
# 将base64字符串解码为二进制数据
decoded_data = base64.b64decode(base64_string)
# 创建一个内存流以读取二进制数据
image_stream = BytesIO(decoded_data)
# 使用PIL打开图像
image = Image.open(image_stream)
# 显示图像
image.show()
text2image = EdenAI(
feature="image",
provider="openai",
params={
"resolution": "512x512"
}
)
image_output = text2image("A cat riding a motorcycle by Picasso")
print_base64_image(image_output)