#!/usr/bin/env python # coding: utf-8 # 在 Colab 中打开 # # # DashVector 矢量存储库 # # 如果您在colab上打开这个笔记本,您可能需要安装LlamaIndex 🦙。 # # In[ ]: get_ipython().run_line_magic('pip', 'install llama-index-vector-stores-dashvector') # In[ ]: get_ipython().system('pip install llama-index') # In[ ]: import logging import sys import os logging.basicConfig(stream=sys.stdout, level=logging.INFO) logging.getLogger().addHandler(logging.StreamHandler(stream=sys.stdout)) # #### 创建一个DashVector集合 # # In[ ]: import dashvector # In[ ]: api_key = os.environ["DASHVECTOR_API_KEY"] client = dashvector.Client(api_key=api_key) # In[ ]: # 文本嵌入ada-002的维度client.create("llama-demo", dimension=1536) # In[ ]: dashvector_collection = client.get("quickstart") # #### 下载数据 # # In[ ]: get_ipython().system("mkdir -p 'data/paul_graham/'") get_ipython().system("wget 'https://raw.githubusercontent.com/run-llama/llama_index/main/docs/docs/examples/data/paul_graham/paul_graham_essay.txt' -O 'data/paul_graham/paul_graham_essay.txt'") # #### 加载文档,构建DashVectorStore和VectorStoreIndex # # In[ ]: from llama_index.core import VectorStoreIndex, SimpleDirectoryReader from llama_index.vector_stores.dashvector import DashVectorStore from IPython.display import Markdown, display # In[ ]: # 加载文档documents = SimpleDirectoryReader("./data/paul_graham").load_data() # In[ ]: # 初始化,不带元数据过滤器from llama_index.core import StorageContextvector_store = DashVectorStore(dashvector_collection)storage_context = StorageContext.from_defaults(vector_store=vector_store)index = VectorStoreIndex.from_documents( documents, storage_context=storage_context) # ```python # # 查询索引 # # 这个代码段演示了如何在Python中查询列表中的元素索引。 # # ``` # # In[ ]: # 将日志级别设置为DEBUG,以获得更详细的输出query_engine = index.as_query_engine()response = query_engine.query("作者在成长过程中做了什么?") # In[ ]: display(Markdown(f"{response}"))