Skip to content

Commit

Permalink
Dynamically Update Provider and Model option
Browse files Browse the repository at this point in the history
  • Loading branch information
arshad-yaseen committed Sep 26, 2024
1 parent a5bef85 commit f878522
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
- [Max Context Lines](#max-context-lines)
- [Copilot Options](#copilot-options)
- [Changing the Provider and Model](#changing-the-provider-and-model)
- [Dynamically Updating Provider and Model](#dynamically-updating-provider-and-model)
- [Custom Model](#custom-model)
- [Completion Request Options](#completion-request-options)
- [Custom Headers](#custom-headers)
Expand Down Expand Up @@ -275,6 +276,17 @@ There are other providers and models available. Here is a list:
| OpenAI | `gpt-4o`, `gpt-4o-mini`, `o1-preview`, `o1-mini` |
| Anthropic | `claude-3.5-Sonnet`, `claude-3-opus`, `claude-3-sonnet`, `claude-3-haiku` |

#### Dynamically Updating Provider and Model

You can change the provider and model at runtime using the `setModel` method.

```javascript
copilot.setModel({
provider: 'anthropic',
model: 'claude-3-haiku',
});
```

### Custom Model

You can use a custom AI model that isn't built into Monacopilot by setting up a `model` when you create a new Copilot. This feature lets you connect to AI models from other services or your own custom-built models.
Expand Down
15 changes: 13 additions & 2 deletions src/classes/copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ import {
CopilotOptions,
CopilotProvider,
CustomCopilotModel,
SetModelOptions,
} from '../types';
import {HTTP, joinWithAnd} from '../utils';

export class Copilot {
private readonly apiKey: string;
private readonly provider: CopilotProvider;
private readonly model: CopilotModel | CustomCopilotModel;
private provider: CopilotProvider;
private model: CopilotModel | CustomCopilotModel;

/**
* Initializes the Copilot instance with an API key and optional configuration.
Expand Down Expand Up @@ -145,4 +146,14 @@ export class Copilot {
return {error: errorDetails.message, completion: null};
}
}

/**
* Sets the model and provider for the Copilot.
* @param options - The options for setting the model and provider.
*/
public setModel({model, provider}: SetModelOptions): void {
this.model = model;
this.provider = provider;
this.validateInputs();
}
}
15 changes: 15 additions & 0 deletions src/types/copilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,21 @@ export type CustomCopilotModel = {
transformResponse: CustomCopilotModelTransformResponse;
};

export type SetModelOptions = {
/**
* The provider to set (e.g., 'openai', 'anthropic', 'groq').
*/
provider: CopilotProvider;
/**
* The model to use for copilot AI requests.
* This can be either:
* 1. A predefined model name (e.g. 'claude-3-opus'): Use this option if you want to use a model that is built into Monacopilot.
* If you choose this option, also set the `provider` property to the corresponding provider of the model.
* 2. A custom model configuration object: Use this option if you want to use a AI model from a third-party service or your own custom model.
*/
model: CopilotModel | CustomCopilotModel;
};

export type CustomCopilotModelConfig = (
apiKey: string,
prompt: {
Expand Down

0 comments on commit f878522

Please sign in to comment.