Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ollama的嵌入模型调用错误 #1305

Closed
5 tasks done
wwjCMP opened this issue Apr 13, 2024 · 6 comments · Fixed by #1715
Closed
5 tasks done

ollama的嵌入模型调用错误 #1305

wwjCMP opened this issue Apr 13, 2024 · 6 comments · Fixed by #1715
Labels
bug Something isn't working

Comments

@wwjCMP
Copy link

wwjCMP commented Apr 13, 2024

例行检查

  • 我已确认目前没有类似 issue
  • 我已确认我已升级到最新版本
  • 我已完整查看过项目 README,尤其是常见问题部分
  • 我理解并愿意跟进此 issue,协助测试和提供反馈
  • 我理解并认可上述内容,并理解项目维护者精力有限,不遵循规则的 issue 可能会被无视或直接关闭

问题描述
docker部署的。设置如下图,当填入聊天模式的时候,测试没有问题。但填入嵌入模式的时候,有400错误。是ollama那边设置的问题吗?
复现步骤

预期结果

相关截图
Snipaste_2024-04-13_11-21-11
Snipaste_2024-04-13_11-12-51
Snipaste_2024-04-13_11-13-56
Snipaste_2024-04-13_11-16-54

#1221
#1159

@wwjCMP wwjCMP added the bug Something isn't working label Apr 13, 2024
@songquanpeng
Copy link
Owner

目前测试只支持测试聊天模型

@songquanpeng
Copy link
Owner

这个之后优化吧,issue 先留着

@nigo81
Copy link

nigo81 commented Apr 21, 2024

同样需要使用ollama的embedding模型nomic。因为使用的mac,使用m3e时只能使用cpu,太慢了,希望能用ollama的nomic模型来作为embedding.

@songquanpeng
Copy link
Owner

同样需要使用ollama的embedding模型nomic。因为使用的mac,使用m3e时只能使用cpu,太慢了,希望能用ollama的nomic模型来作为embedding.

这个接口不应该是一样的么?

@LuckyGJX
Copy link

同样连接拒绝,使用ollama,希望尽快支持~
image

@RexWzh
Copy link

RexWzh commented Apr 23, 2024

ollama 文档写了后续会把 embedding 也 OpenAI 化,OneAPI 完全没必要重复造轮子。
image

如果实在急着用,自己手写个 flask。参考个人写过的一段代码,如果懒得改,连同 Ollama 当前的调用方式丢给 GPT 转一下:

from flask import Flask, request, jsonify
from sentence_transformers import SentenceTransformer
app = Flask(__name__)
# 加载模型
model_path = '/sshfs/pretrains/moka-ai/m3e-large'
model = SentenceTransformer(model_path)

@app.route('/v1/embeddings', methods=['POST'])
def get_embeddings():
    data = request.get_json()
    if not all((data, 'input' in data, 'model' in data)):
        return jsonify({"error": "Missing required fields in request body"}), 400
    input_text = data['input']
    # 将单个输入转换为列表
    if isinstance(input_text, str):
        input_text = [input_text]
    # 调用模型获取 embeddings
    embeddings = model.encode(input_text)
    # 构造响应
    embeddings_response = []
    for index, embedding in enumerate(embeddings):
        embedding_dict = {
            "object": "embedding",
            "embedding": embedding.tolist(),  # 将 numpy 数组转换为列表
            "index": index
        }
        embeddings_response.append(embedding_dict)
    # 可选: Token 计数
    response = {
        "object": "list",
        "data": embeddings_response,
        "model": data['model'],
        "usage": {
            "prompt_tokens": sum(len(text.split()) for text in input_text), 
            "total_tokens": sum(len(text.split()) for text in input_text)
        }
    }
    return jsonify(response)

if __name__ == '__main__':
    app.run(debug=True, port=5000, host='0.0.0.0')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants