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

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

Closed
1 task done
hikerell opened this issue Aug 29, 2024 · 3 comments · Fixed by #8065
Closed
1 task done

Comments

@hikerell
Copy link

hikerell commented Aug 29, 2024

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?

  • Yes

Relevant log output

No response

Anything else?

No response

@affine-issue-bot
Copy link

Issue Status: 🆕 *Untriaged

*🆕 Untriaged

The team has not yet reviewed the issue. We usually do it within one business day.
Docs: https://github.com/toeverything/AFFiNE/blob/canary/docs/issue-triaging.md

This is an automatic reply by the bot.

@toeverything toeverything deleted a comment Aug 29, 2024
@forehalo
Copy link
Member

forehalo commented Sep 3, 2024

we haven't test through openai compatible apis, @darkskygit right?

@darkskygit
Copy link
Member

according to openai's doc, the array will have least one element, it may be due to non-standard impl by third party.
but for minor implementation deviations, i think add fault tolerance to support them is not bad
i will make a pr later

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🆕 *Untriaged
Development

Successfully merging a pull request may close this issue.

3 participants