AFFiNE icon indicating copy to clipboard operation
AFFiNE copied to clipboard

​fix openai copilot provider message parser bug to support Azure OpenAI deploment

Open hikerell opened this issue 6 months ago • 1 comments

What happened?

the coploit provider support openai means that Azure OpenAI also is useable with special config:

// ~/.affine/self-host/config/affine.js

AFFiNE.use('copilot', {
    openai: {
        baseURL: "https://YOUR_AZURE_OPENAI_REGION.openai.azure.com/openai/deployments/YOUR_AZURE_OPENAI_DEPLOYMENT_NAME",
        apiKey: "YOUR_AZURE_OPENAI_API_KEY",
        defaultQuery: {"api-version": "2024-02-15-preview" },
       defaultHeaders: {"api-key": "YOUR_AZURE_OPENAI_API_KEY"},
     },
}

The azure openai chat completion with streaming mode will return a empty "choices" at first time. It make openai coploit privoder throw an error:

copilot_provider_side_error: Provider openai failed with unexpected_response error: Cannot read properties of undefined (reading 'delta')
    at OpenAIProvider.handleError (file:///app/dist/plugins/copilot/providers/openai.js:131:20)
    at OpenAIProvider.generateTextStream (file:///app/dist/plugins/copilot/providers/openai.js:191:24)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

And the solution is very simple, just add a '?' at the follow location:

// plugins/copilot/providers/openai.ts

        for await (const message of result) {
 -        const content = message.choices[0].delta.content;
 +        const content = message.choices[0]?.delta.content;
          if (content) {
            yield content;
            if (options.signal?.aborted) {
              result.controller.abort();
              break;
            }
          }
        }

NOTE: Because I deploy my privite affine project with offical docker image, I fix the above bug by updating file /app/dist/plugins/copilot/providers/openai.js directly.

Distribution version

Web (app.affine.pro)

What browsers are you seeing the problem on if you're using web version?

No response

Are you self-hosting?

  • [X] Yes

Relevant log output

No response

Anything else?

No response

hikerell avatar Aug 29 '24 04:08 hikerell