将 `apify_api_token` 作为命名参数传递

问题描述 投票:0回答:1

我是 python 新手,我正在尝试将 API 密钥传递给笔记本中的 apifywrapper。我收到此错误:

ValidationError Traceback(最近调用 最后)在 () 7 api_token = userdata.get("apify") 8 ----> 9 apify = ApifyWrapper(apify_api_token=api_token) 10 # 调用Actor从爬取的网页中获取文本 11 加载器 = apify.call_actor(

/usr/local/lib/python3.10/dist-packages/pydantic/main.cpython-310-x86_64-linux-gnu.so 在 pydantic.main.BaseModel.init()

ValidationError:ApifyWrapper 的 1 个验证错误 root 没有找到apify_api_token,请添加包含它的环境变量

APIFY_API_TOKEN
,或者通过
apify_api_token
作为命名参数。 (类型=值_错误)

!pip install apify apify-client 
from langchain.docstore.document import Document
from langchain.indexes import VectorstoreIndexCreator
from langchain.utilities import ApifyWrapper
import apify

api_token = userdata.get("apify")
apify = ApifyWrapper(apify_api_token=api_token)
# Call the Actor to obtain text from the crawled webpages
loader = apify.call_actor(
    actor_id="apify/website-content-crawler",
    run_input={"startUrls": [{"url": "https://python.langchain.com/docs/integrations/chat/"}]},
    dataset_mapping_function=lambda item: Document(
        page_content=item["text"] or "", metadata={"source": item["url"]}
    ),
)

可以向我解释我做错了什么以及如何解决这个问题吗? 泰

python google-colaboratory apify
1个回答
0
投票

出现错误“ValidationError:未找到 apify_api_token,请添加包含它的环境变量 APIFY_API_TOKEN,或将 apify_api_token 作为命名参数传递”,因为缺少 APIFY_API_TOKEN 环境变量。要解决此问题,您可以在创建 ApifyWrapper 类的实例时设置 APIFY_API_TOKEN 环境变量或将 apify_api_token 作为命名参数传递。 以下是如何在 Python 中设置环境变量:

import os
os.environ["APIFY_API_TOKEN"] = "[your API token]"
apify = ApifyWrapper( )
© www.soinside.com 2019 - 2024. All rights reserved.