{"cells": [{"attachments": {}, "cell_type": "markdown", "metadata": {}, "source": ["\"在\n"]}, {"cell_type": "markdown", "metadata": {}, "source": ["# 使用Clarifai进行嵌入\n", "\n", "LlamaIndex支持Clarifai嵌入模型。\n"]}, {"cell_type": "markdown", "metadata": {}, "source": ["您必须拥有Clarifai账户和个人访问令牌(PAT)密钥。\n", "[在这里检查](https://clarifai.com/settings/security)获取或创建PAT。\n", "\n", "将CLARIFAI_PAT设置为环境变量,或者您可以将PAT作为参数传递给ClarifaiEmbedding类。\n"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["%pip install llama-index-embeddings-clarifai"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["!export CLARIFAI_PAT=YOUR_KEY"]}, {"attachments": {}, "cell_type": "markdown", "metadata": {}, "source": ["如果您在colab上打开这个笔记本,您可能需要安装LlamaIndex 🦙。\n"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["!pip install llama-index"]}, {"cell_type": "markdown", "metadata": {}, "source": ["模型可以通过完整的URL或者通过模型名称、用户ID和应用程序ID的组合进行引用。\n"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["from llama_index.embeddings.clarifai import ClarifaiEmbedding", "", "# 创建一个 Clarifai 嵌入类,只需使用 model_url,假设 CLARIFAI_PAT 已设置为环境变量", "embed_model = ClarifaiEmbedding(", " model_url=\"https://clarifai.com/clarifai/main/models/BAAI-bge-base-en\"", ")", "", "# 或者您也可以使用 model_name、user_id、app_id 和 pat 来初始化类。", "embed_model = ClarifaiEmbedding(", " model_name=\"BAAI-bge-base-en\",", " user_id=\"clarifai\",", " app_id=\"main\",", " pat=CLARIFAI_PAT,", ")"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["embeddings = embed_model.get_text_embedding(\"Hello World!\")\n", "print(len(embeddings))\n", "print(embeddings[:5])"]}, {"cell_type": "markdown", "metadata": {}, "source": ["嵌入文本列表\n"]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["text = \"roses are red violets are blue.\"\n", "text2 = \"Make hay while the sun shines.\""]}, {"cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": ["embeddings = embed_model._get_text_embeddings([text2, text])\n", "print(len(embeddings))\n", "print(embeddings[0][:5])\n", "print(embeddings[1][:5])"]}], "metadata": {"language_info": {"name": "python"}}, "nbformat": 4, "nbformat_minor": 2}