ollama embedding兼容OpenAI格式

ollama embeddings 端点:/api/embed,而非/embeddings,输出的格式与OpenAI不兼容,下面给出兼容的方式。推荐模型: bge-m3

import json

# Ollama 嵌入数据
ollama_embeddings = {
    "model": "all-minilm",
    "embeddings": [
        [
            0.010071029, -0.0017594862, 0.05007221, 0.04692972, 0.054916814,
            0.008599704, 0.105441414, -0.025878139, 0.12958129, 0.031952348
        ],
        [
            -0.0098027075, 0.06042469, 0.025257962, -0.006364387, 0.07272725,
            0.017194884, 0.09032035, -0.051705178, 0.09951512, 0.09072481
        ]
    ]
}

# 转换为 OpenAI 嵌入输出格式
openai_embeddings = {
    "data": [
        {
            "embedding": embedding,
            "index": index,
            "object": "embedding"
        }
        for index, embedding in enumerate(ollama_embeddings["embeddings"])
    ]
}

# 打印转换后的 JSON 格式
print(json.dumps(openai_embeddings, indent=2))

输出结果:

{
  "data": [
    {
      "embedding": [
        0.010071029,
        -0.0017594862,
        0.05007221,
        0.04692972,
        0.054916814,
        0.008599704,
        0.105441414,
        -0.025878139,
        0.12958129,
        0.031952348
      ],
      "index": 0,
      "object": "embedding"
    },
    {
      "embedding": [
        -0.0098027075,
        0.06042469,
        0.025257962,
        -0.006364387,
        0.07272725,
        0.017194884,
        0.09032035,
        -0.051705178,
        0.09951512,
        0.09072481
      ],
      "index": 1,
      "object": "embedding"
    }
  ]
}

### 如何调用Embedding API 为了有效地利用OpenAIEmbeddings服务,了解其基本概念和具体实现方法至关重要。以下是关于如何调用Embedding API的具体指南。 #### 调用流程概述 当准备就绪要请求嵌入向量时,需构建HTTP POST请求发送至指定端点。此过程涉及设置必要的头部信息以及定义负载中的参数[^2]。 #### 请求结构 - **URL**: `https://api.openai.com/v1/embeddings` - **Method**: POST - **Headers**: - `Content-Type`: application/json - `Authorization`: Bearer YOUR_API_KEY (替换为实际API密钥) #### 参数配置 在POST请求体中传递JSON对象作为输入数据: | 字段名 | 类型 | 描述 | | --- | --- | --- | | model | string | 所使用的模型名称,默认可选`text-similarity-davinci-001`等版本 | | input | array of strings or string | 待转换成嵌入表示形式的文字内容 | 示例如下所示: ```json { "model": "text-similarity-davinci-001", "input": ["这句话会被转化为嵌入"] } ``` #### Python代码实例 下面是一个简单的Python脚本用于演示如何发起上述类型的API请求并解析返回的结果: ```python import requests import json def get_embedding(text, api_key): url = 'https://api.openai.com/v1/embeddings' headers = { 'Content-Type': 'application/json', 'Authorization': f'Bearer {api_key}' } payload = { "model": "text-similarity-davinci-001", "input": text } response = requests.post(url=url, headers=headers, data=json.dumps(payload)) if response.status_code == 200: result = response.json() embedding = result['data'][0]['embedding'] return embedding else: raise Exception(f"Error calling embeddings endpoint: {response.text}") # Example usage if __name__ == "__main__": sample_text = ["这是一些测试文本."] my_api_key = "<your_openai_api_key_here>" try: emb_result = get_embedding(sample_text, my_api_key) print(emb_result[:5]) # Print first five elements as a preview. except Exception as e: print(e) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jacky_wxl(微信同号)

喜欢作者

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值