Files
wehub-resource-sync a0c8464e58
Build Package / build (ubuntu-latest) (push) Failing after 1s
CodeQL / Analyze (python) (push) Failing after 1s
Core Typecheck / core-typecheck (push) Failing after 1s
Linting / lint (push) Failing after 1s
llama-dev tests / test-llama-dev (push) Failing after 1s
Publish Sub-Package to PyPI if Needed / publish_subpackage_if_needed (push) Has been skipped
Sync Docs to Developer Hub / sync-docs (push) Failing after 0s
Build Package / build (windows-latest) (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 12:26:52 +08:00

264 lines
8.4 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a href=\"https://colab.research.google.com/github/run-llama/llama_index/blob/main/docs/examples/embeddings/dashscope_embeddings.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# DashScope Embeddings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"If you're opening this Notebook on colab, you will probably need to install LlamaIndex 🦙."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%pip install llama-index-core\n",
"%pip install llama-index-embeddings-dashscope"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Set API key\n",
"%env DASHSCOPE_API_KEY=YOUR_DASHSCOPE_API_KEY\n",
"\n",
"# you can set API key parameter DashScopeTextEmbedding(model=DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2, api_key=api_key)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dimension of embeddings: 1536\n",
"Input: 风急天高猿啸哀, embedding is: [-0.0016666285653348784, 0.008690492014557004, 0.02894828715284365, -0.01774133615134858, 0.03627544697161321]\n",
"Dimension of embeddings: 1536\n",
"Input: 渚清沙白鸟飞回, embedding is: [0.018255604113922633, 0.030631669725945727, 0.0031333343045102462, 0.014323813963475412, 0.009666154862176396]\n",
"Dimension of embeddings: 1536\n",
"Input: 无边落木萧萧下, embedding is: [-0.01270165436681136, 0.011355212676752505, -0.007090375205285297, 0.008317427977013809, 0.0341982923839579]\n",
"Dimension of embeddings: 1536\n",
"Input: 不尽长江滚滚来, embedding is: [0.003449439128962428, 0.02667092110022496, -0.0010223853088419568, -0.00971414215183749, 0.0035561228133633277]\n"
]
}
],
"source": [
"# imports\n",
"from llama_index.embeddings.dashscope import (\n",
" DashScopeEmbedding,\n",
" DashScopeTextEmbeddingModels,\n",
" DashScopeTextEmbeddingType,\n",
")\n",
"\n",
"# Create embeddings\n",
"# text_type=`document` to build index\n",
"embedder = DashScopeEmbedding(\n",
" model_name=DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2,\n",
" text_type=DashScopeTextEmbeddingType.TEXT_TYPE_DOCUMENT,\n",
")\n",
"text_to_embedding = [\"风急天高猿啸哀\", \"渚清沙白鸟飞回\", \"无边落木萧萧下\", \"不尽长江滚滚来\"]\n",
"# Call text Embedding\n",
"result_embeddings = embedder.get_text_embedding_batch(text_to_embedding)\n",
"# requests and embedding result index is correspond to.\n",
"for index, embedding in enumerate(result_embeddings):\n",
" if embedding is None: # if the correspondence request is embedding failed.\n",
" print(\"The %s embedding failed.\" % text_to_embedding[index])\n",
" else:\n",
" print(\"Dimension of embeddings: %s\" % len(embedding))\n",
" print(\n",
" \"Input: %s, embedding is: %s\"\n",
" % (text_to_embedding[index], embedding[:5])\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dimension of embeddings: 1536\n",
"[-0.00838587212517078, 0.01004877272531103, 0.0015754734226650637, -0.04273583173235969, -0.05209946086276315]\n"
]
}
],
"source": [
"# imports\n",
"from llama_index.embeddings.dashscope import (\n",
" DashScopeEmbedding,\n",
" DashScopeTextEmbeddingModels,\n",
" DashScopeTextEmbeddingType,\n",
")\n",
"\n",
"# Create embeddings\n",
"# text_type=`query` to retrive relevant context.\n",
"embedder = DashScopeEmbedding(\n",
" model_name=DashScopeTextEmbeddingModels.TEXT_EMBEDDING_V2,\n",
" text_type=DashScopeTextEmbeddingType.TEXT_TYPE_QUERY,\n",
")\n",
"# Call text Embedding\n",
"embedding = embedder.get_text_embedding(\"衣服的质量杠杠的,很漂亮,不枉我等了这么久啊,喜欢,以后还来这里买\")\n",
"print(f\"Dimension of embeddings: {len(embedding)}\")\n",
"print(embedding[:5])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"https://dashscope-result-bj.oss-cn-beijing.aliyuncs.com/5fc5c860/2024-01-29/644ccedb-0b14-481c-a975-16bb5249282d_output_1706517940902.txt.gz?Expires=1706777144&OSSAccessKeyId=LTAI5tQZd8AEcZX6KZV4G8qL&Signature=g%2B0qcmOSwxEj8Cb2zXlvBbA6Fas%3D\n"
]
}
],
"source": [
"# call batch text embedding\n",
"from llama_index.embeddings.dashscope import (\n",
" DashScopeEmbedding,\n",
" DashScopeBatchTextEmbeddingModels,\n",
" DashScopeTextEmbeddingType,\n",
")\n",
"\n",
"embedder = DashScopeEmbedding(\n",
" model_name=DashScopeBatchTextEmbeddingModels.TEXT_EMBEDDING_ASYNC_V2,\n",
" text_type=DashScopeTextEmbeddingType.TEXT_TYPE_DOCUMENT,\n",
")\n",
"\n",
"embedding_result_file_url = embedder.get_batch_text_embedding(\n",
" embedding_file_url=\"https://dashscope.oss-cn-beijing.aliyuncs.com/samples/text/text-embedding-test.txt\"\n",
")\n",
"print(embedding_result_file_url)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dimension of embeddings: 1536\n",
"[-0.03515625, 0.05035400390625, 0.008087158203125, 0.0163116455078125, 0.01064300537109375]\n"
]
}
],
"source": [
"# call multimodal embedding service\n",
"from llama_index.embeddings.dashscope import (\n",
" DashScopeEmbedding,\n",
" DashScopeMultiModalEmbeddingModels,\n",
")\n",
"\n",
"embedder = DashScopeEmbedding(\n",
" model_name=DashScopeMultiModalEmbeddingModels.MULTIMODAL_EMBEDDING_ONE_PEACE_V1,\n",
")\n",
"\n",
"embedding = embedder.get_image_embedding(\n",
" img_file_path=\"https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png\"\n",
")\n",
"print(f\"Dimension of embeddings: {len(embedding)}\")\n",
"print(embedding[:5])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Dimension of embeddings: 1536\n",
"[-0.0200169887393713, 0.041749317198991776, 0.01004155445843935, 0.03983306884765625, -0.006652673240751028]\n"
]
}
],
"source": [
"# call multimodal embedding service\n",
"from llama_index.embeddings.dashscope import (\n",
" DashScopeEmbedding,\n",
" DashScopeMultiModalEmbeddingModels,\n",
")\n",
"\n",
"embedder = DashScopeEmbedding(\n",
" model_name=DashScopeMultiModalEmbeddingModels.MULTIMODAL_EMBEDDING_ONE_PEACE_V1,\n",
")\n",
"\n",
"input = [\n",
" {\"factor\": 1, \"text\": \"你好\"},\n",
" {\n",
" \"factor\": 2,\n",
" \"audio\": \"https://dashscope.oss-cn-beijing.aliyuncs.com/audios/cow.flac\",\n",
" },\n",
" {\n",
" \"factor\": 3,\n",
" \"image\": \"https://dashscope.oss-cn-beijing.aliyuncs.com/images/256_1.png\",\n",
" },\n",
"]\n",
"\n",
"embedding = embedder.get_multimodal_embedding(input=input)\n",
"print(f\"Dimension of embeddings: {len(embedding)}\")\n",
"print(embedding[:5])"
]
}
],
"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"
},
"vscode": {
"interpreter": {
"hash": "b0fa6594d8f4cbf19f97940f81e996739fb7646882a419484c72d19e05852a7e"
}
}
},
"nbformat": 4,
"nbformat_minor": 4
}