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

Add Azure OpenAI support #96

Closed
wants to merge 11 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,13 @@ Once Prettier is installed, you can run it on a single file or multiple files us
1. For a single file:

```sh
prettier --write path/to/your/file.js
prettier --write path/to/your/file.ts
```

2. For a multiple file:

```sh
prettier --write "src/**/*.js"
prettier --write "src/**/*.ts"
```

If you use Vscode, It is recommended to use [prettier-vscode][https://github.com/prettier/prettier-vscode]
217 changes: 214 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"version": "1.0.6",
"type": "module",
"dependencies": {
"@azure/openai": "^1.0.0-beta.5",
"@clack/core": "latest",
"@clack/prompts": "latest",
"@dqbd/tiktoken": "^1.0.2",
Expand Down
4 changes: 3 additions & 1 deletion src/commands/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ export default command(
role: 'assistant',
content: fullResponse,
});
console.log('');
if (!fullResponse.endsWith('\n')) {
console.log('');
}
console.log('');
prompt();
};
Expand Down
23 changes: 18 additions & 5 deletions src/helpers/completion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import type { AxiosError } from 'axios';
import { streamToString } from './stream-to-string';
import './replace-all-polyfill';
import i18n from './i18n';
import { getConfig } from './config';

import { OpenAIClient, AzureKeyCredential } from '@azure/openai';

const explainInSecondRequest = true;

Expand All @@ -18,9 +21,9 @@ function getOpenAi(key: string, apiEndpoint: string) {
return openAi;
}

// Openai outputs markdown format for code blocks. It oftne uses
// Openai outputs markdown format for code blocks. It often uses
// a github style like: "```bash"
const shellCodeStartRegex = /```[^\n]*/gi;
const shellCodeStartRegex = /```[^\n]*\n?/gi;

export async function getScriptAndInfo({
prompt,
Expand Down Expand Up @@ -66,8 +69,20 @@ export async function generateCompletion({
key: string;
apiEndpoint: string;
}) {
const openAi = getOpenAi(key, apiEndpoint);
try {
if (apiEndpoint.endsWith('.openai.azure.com')) {
const { AZURE_OPENAI_DEPLOYMENT: deployment } = await getConfig();
const messages = Array.isArray(prompt)
? prompt
: [{ role: 'user', content: prompt }];
const client = new OpenAIClient(apiEndpoint, new AzureKeyCredential(key));
const deploymentId = deployment;
const events = client.listChatCompletions(deploymentId, messages, {
maxTokens: 1024,
});
return events;
}
const openAi = getOpenAi(key, apiEndpoint);
const completion = await openAi.createChatCompletion(
{
model: model || 'gpt-3.5-turbo',
Expand Down Expand Up @@ -216,14 +231,12 @@ export const readData =
}
if (excluded) break;
}

if (content && waitUntilNewline) {
if (!content.includes('\n')) {
continue;
}
waitUntilNewline = false;
}

if (dataStart && content) {
const contentWithoutExcluded = excluded
? content.replaceAll(excluded, '')
Expand Down
Loading
Loading