vercel / ai

Build AI-powered applications with React, Svelte, Vue, and Solid
https://sdk.vercel.ai/docs
Other
10.09k stars 1.5k forks source link

support non-float embeddings #3652

Open Nishchit14 opened 2 days ago

Nishchit14 commented 2 days ago

Description

Cohere embeddings are not working other than default float.

I am trying to create the binary embeddings but it generates the invalid response error from the SDK.

I am using custom fetch

  return createCohere({
    apiKey: process.env.EMBEDING_PROVIDER_KEY,
    fetch: async (url, options) => {
      const body = JSON.parse(options!.body! as string);
      console.log(url, "cohere embeded url", options);
      return fetch(url, {
        ...options,
        body: JSON.stringify({ ...body, embedding_types: ["binary"] }),
      });
    },
  });

This is the error from SDK

  Error message: [
    {
      "code": "invalid_type",
      "expected": "array",
      "received": "undefined",
      "path": [
        "embeddings",
        "float"
      ],
      "message": "Required"
    }
  ]

This is the console resonse from the cohere

{
    "id":"7dcc3ce4-ee3c-49f6-8fe1-fb5d59d78954",
    "texts":["Hello Word!"],
    "embeddings":
        {
            "binary":[[112,-18,124,48,83,87,-50,48,124,15,-15,-76,33,-61,-120,-90,90,-75,-69,25,2,-96,109,84,-5,62,-44,-69,-96,-16,124,22,34,-102,-11,117,-123,-58,108,3,-122,67,108,99,-30,7,-19,-115,107,119,9,80,-64,110,1,-3,-126,82,-99,-75,-17,50,15,-68,-50,-51,-64,127,5,-78,-75,-67,54,-70,127,-122,126,-9,-32,0,-96,-31,80,-37,112,-56,29,-108,39,29,-32,-40,-82,-45,76,-58,-5,-14,124,26,19,-48,84,108,47,45,-10,-6,92,57,33,-7,18,-69,92,-47,16,8,90,27,-10,-47,123,104,-69,-110,-56,28]]
         },
     "meta":{"api_version":{"version":"2"},
     "billed_units":{"input_tokens":3}},
     "response_type":"embeddings_by_type"
},

Code example

return createCohere({ apiKey: process.env.EMBEDING_PROVIDER_KEY, fetch: async (url, options) => { const body = JSON.parse(options!.body! as string); console.log(url, "cohere embeded url", options); return fetch(url, { ...options, body: JSON.stringify({ ...body, embedding_types: ["binary"] }), }); }, });

AI provider

@ai-sdk/cohere v.0.0.28

Additional context

No response

Nishchit14 commented 2 days ago

Here is the code reference https://github.com/vercel/ai/blob/c5ff26dbb444fe179732d0a96834ca9fb0519002/packages/cohere/src/cohere-embedding-model.ts#L70

It seems like the only float value is supported for now.

lgrammel commented 2 days ago

The AI SDK currently only supports float embeddings.

lgrammel commented 2 days ago

https://github.com/vercel/ai/pull/3654