DeepInfra¶
通过此集成,您可以使用 DeepInfra 的嵌入模型为文本数据获取嵌入向量。这里是嵌入模型的链接。
首先,您需要在 DeepInfra 网站 上注册并获取 API 令牌。您可以从模型卡片中复制 model_ids
,然后开始在代码中使用它们。
安装¶
In [ ]:
Copied!
!pip install llama-index llama-index-embeddings-deepinfra
!pip install llama-index llama-index-embeddings-deepinfra
初始化¶
In [ ]:
Copied!
from dotenv import load_dotenv, find_dotenv
from llama_index.embeddings.deepinfra import DeepInfraEmbeddingModel
_ = load_dotenv(find_dotenv())
model = DeepInfraEmbeddingModel(
model_id="BAAI/bge-large-en-v1.5", # Use custom model ID
api_token="YOUR_API_TOKEN", # Optionally provide token here
normalize=True, # Optional normalization
text_prefix="text: ", # Optional text prefix
query_prefix="query: ", # Optional query prefix
)
from dotenv import load_dotenv, find_dotenv
from llama_index.embeddings.deepinfra import DeepInfraEmbeddingModel
_ = load_dotenv(find_dotenv())
model = DeepInfraEmbeddingModel(
model_id="BAAI/bge-large-en-v1.5", # Use custom model ID
api_token="YOUR_API_TOKEN", # Optionally provide token here
normalize=True, # Optional normalization
text_prefix="text: ", # Optional text prefix
query_prefix="query: ", # Optional query prefix
)
同步请求¶
获取文本嵌入向量¶
In [ ]:
Copied!
response = model.get_text_embedding("hello world")
print(response)
response = model.get_text_embedding("hello world")
print(response)
批量请求¶
In [ ]:
Copied!
texts = ["hello world", "goodbye world"]
response_batch = model.get_text_embedding_batch(texts)
print(response_batch)
texts = ["hello world", "goodbye world"]
response_batch = model.get_text_embedding_batch(texts)
print(response_batch)
查询请求¶
In [ ]:
Copied!
query_response = model.get_query_embedding("hello world")
print(query_response)
query_response = model.get_query_embedding("hello world")
print(query_response)
异步请求¶
获取文本嵌入向量¶
In [ ]:
Copied!
async def main():
text = "hello world"
async_response = await model.aget_text_embedding(text)
print(async_response)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
async def main():
text = "hello world"
async_response = await model.aget_text_embedding(text)
print(async_response)
if __name__ == "__main__":
import asyncio
asyncio.run(main())
如有任何问题或反馈,请通过 feedback@deepinfra.com 联系我们。