{"cells": [{"attachments": {}, "cell_type": "markdown", "id": "2162b9f1", "metadata": {}, "source": ["\"在\n"]}, {"attachments": {}, "cell_type": "markdown", "id": "36e7bb96-0c27-47e9-a525-c11f40be3b86", "metadata": {}, "source": ["# Weaviate Reader\n"]}, {"cell_type": "code", "execution_count": null, "id": "a235ac8a", "metadata": {}, "outputs": [], "source": ["%pip install llama-index-readers-weaviate"]}, {"cell_type": "code", "execution_count": null, "id": "38ca1434", "metadata": {}, "outputs": [], "source": ["import logging\n", "import sys\n", "\n", "logging.basicConfig(stream=sys.stdout, level=logging.INFO)\n", "logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout))"]}, {"attachments": {}, "cell_type": "markdown", "id": "4d1da511", "metadata": {}, "source": ["如果您在colab上打开这个笔记本,您可能需要安装LlamaIndex 🦙。\n"]}, {"cell_type": "code", "execution_count": null, "id": "5ec37a7c", "metadata": {}, "outputs": [], "source": ["!pip install llama-index"]}, {"cell_type": "code", "execution_count": null, "id": "d99bc57b-85df-46ac-8262-2409344af428", "metadata": {}, "outputs": [], "source": ["import weaviate\n", "from llama_index.readers.weaviate import WeaviateReader"]}, {"cell_type": "code", "execution_count": null, "id": "fec36c7a-3766-4167-890e-b93adb831a64", "metadata": {}, "outputs": [], "source": ["# 有关身份验证的更多详细信息,请参阅https://weaviate.io/developers/weaviate/current/client-libraries/python.html\n", "resource_owner_config = weaviate.AuthClientPassword(\n", " username=\"<用户名>\",\n", " password=\"<密码>\",\n", ")\n", "\n", "# 初始化读取器\n", "reader = WeaviateReader(\n", " \"https://.semi.network/\",\n", " auth_client_secret=resource_owner_config,\n", ")"]}, {"cell_type": "markdown", "id": "ce9f299c-4f0a-4bca-bc90-79848f02b381", "metadata": {}, "source": ["您有两种选项可以使用Weaviate阅读器:1)直接指定class_name和properties,或者2)输入原始的graphql_query。下面展示了示例。\n"]}, {"cell_type": "code", "execution_count": null, "id": "b92d69a1-d39f-45cf-a136-cb9c2f2f5cdf", "metadata": {}, "outputs": [], "source": ["# 1) 使用class_name和properties加载数据\n", "# docs = reader.load_data(\n", "# class_name=\"Author\", properties=[\"name\", \"description\"], separate_documents=True\n", "# )\n", "\n", "documents = reader.load_data(\n", " class_name=\"<类名>\",\n", " properties=[\"属性1\", \"属性2\", \"…\"],\n", " separate_documents=True,\n", ")"]}, {"cell_type": "code", "execution_count": null, "id": "722b5d47-9897-4c54-9734-259ab0c1634c", "metadata": {}, "outputs": [], "source": ["# 2) 示例 GraphQL 查询\n", "# 查询 = \"\"\"\n", "# {\n", "# Get {\n", "# 作者 {\n", "# 名字\n", "# 描述\n", "# }\n", "# }\n", "# }\n", "# \"\"\"\n", "# 文档 = reader.load_data(graphql_query=query, separate_documents=True)\n", "\n", "查询 = \"\"\"\n", "{\n", " Get {\n", " <类名> {\n", " <属性1>\n", " <属性2>\n", " ...\n", " }\n", " }\n", "}\n", "\"\"\"\n", "\n", "文档 = reader.load_data(graphql_query=query, separate_documents=True)"]}, {"cell_type": "markdown", "id": "169b4273-eb20-4d06-9ffe-71320f4570f6", "metadata": {}, "source": ["### 创建索引\n"]}, {"cell_type": "code", "execution_count": null, "id": "92599a0a-93ba-4c93-80f1-9acae0663c34", "metadata": {}, "outputs": [], "source": ["index = SummaryIndex.from_documents(documents)"]}, {"cell_type": "code", "execution_count": null, "id": "52d93c3f-a08d-4637-98bc-0c3cc693c563", "metadata": {}, "outputs": [], "source": ["# 将日志级别设置为DEBUG,以获得更详细的输出\n", "query_engine = index.as_query_engine()\n", "response = query_engine.query(\"\")"]}, {"cell_type": "code", "execution_count": null, "id": "771b42be-4108-43a0-a1b4-b259a7819936", "metadata": {}, "outputs": [], "source": ["display(Markdown(f\"{response}\"))"]}], "metadata": {"kernelspec": {"display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3"}, "language_info": {"codemirror_mode": {"name": "ipython", "version": 3}, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3"}}, "nbformat": 4, "nbformat_minor": 5}