Skip to content

Commit

Permalink
Cleanup provider constructors
Browse files Browse the repository at this point in the history
Makes constructors for the different ai providers take in objects instead of a bunch of properties so it's clearer when initializing
  • Loading branch information
flakey5 committed Apr 13, 2024
1 parent dcc1a2b commit 14f83bf
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 12 deletions.
7 changes: 6 additions & 1 deletion ai-providers/mistral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,17 @@ class MistralByteSource implements UnderlyingByteSource {
}
}

type MistralProviderCtorOptions = {

Check failure on line 55 in ai-providers/mistral.ts

View workflow job for this annotation

GitHub Actions / Linting

Use an `interface` instead of a `type`

Check failure on line 55 in ai-providers/mistral.ts

View workflow job for this annotation

GitHub Actions / Linting

Use an `interface` instead of a `type`
model: string,

Check failure on line 56 in ai-providers/mistral.ts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected separator

Check failure on line 56 in ai-providers/mistral.ts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected separator
apiKey: string
}

export class MistralProvider implements AiProvider {
model: string
apiKey: string
client?: import('@mistralai/mistralai').default = undefined

constructor (model: string, apiKey: string) {
constructor ({ model, apiKey }: MistralProviderCtorOptions) {
this.model = model
this.apiKey = apiKey
}
Expand Down
7 changes: 6 additions & 1 deletion ai-providers/ollama.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ class OllamaByteSource implements UnderlyingByteSource {
}
}

type OllamaProviderCtorOptions = {

Check failure on line 40 in ai-providers/ollama.ts

View workflow job for this annotation

GitHub Actions / Linting

Use an `interface` instead of a `type`

Check failure on line 40 in ai-providers/ollama.ts

View workflow job for this annotation

GitHub Actions / Linting

Use an `interface` instead of a `type`
host: string,

Check failure on line 41 in ai-providers/ollama.ts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected separator

Check failure on line 41 in ai-providers/ollama.ts

View workflow job for this annotation

GitHub Actions / Linting

Unexpected separator
model: string
}

export class OllamaProvider implements AiProvider {
model: string
client: Ollama

constructor (host: string, model: string) {
constructor ({ host, model }: OllamaProviderCtorOptions) {
this.model = model
this.client = new Ollama({ host })
}
Expand Down
7 changes: 6 additions & 1 deletion ai-providers/open-ai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,16 @@ class OpenAiByteSource implements UnderlyingByteSource {
}
}

type OpenAiProviderCtorOptions = {

Check failure on line 84 in ai-providers/open-ai.ts

View workflow job for this annotation

GitHub Actions / Linting

Use an `interface` instead of a `type`

Check failure on line 84 in ai-providers/open-ai.ts

View workflow job for this annotation

GitHub Actions / Linting

Use an `interface` instead of a `type`
model: string
apiKey: string
}

export class OpenAiProvider implements AiProvider {
model: string
client: OpenAI

constructor (model: string, apiKey: string) {
constructor ({ model, apiKey }: OpenAiProviderCtorOptions) {
this.model = model
// @ts-expect-error
this.client = new OpenAI({ apiKey, fetch })
Expand Down
9 changes: 3 additions & 6 deletions plugins/warp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ const UnknownAiProviderError = createError('UNKNOWN_AI_PROVIDER', 'Unknown AI Pr

function build (aiProvider: AiWarpConfig['aiProvider']): AiProvider {
if ('openai' in aiProvider) {
const { model, apiKey } = aiProvider.openai
return new OpenAiProvider(model, apiKey)
return new OpenAiProvider(aiProvider.openai)
} else if ('mistral' in aiProvider) {
const { model, apiKey } = aiProvider.mistral
return new MistralProvider(model, apiKey)
return new MistralProvider(aiProvider.mistral)
} else if ('ollama' in aiProvider) {
const { host, model } = aiProvider.ollama
return new OllamaProvider(host, model)
return new OllamaProvider(aiProvider.ollama)
} else {
throw new UnknownAiProviderError()
}
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/ai-providers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import { OllamaProvider } from '../../ai-providers/ollama'
const expectedStreamBody = buildExpectedStreamBodyString()

const providers: AiProvider[] = [
new OpenAiProvider('gpt-3.5-turbo', ''),
new MistralProvider('open-mistral-7b', ''),
new OllamaProvider(OLLAMA_MOCK_HOST, 'some-model')
new OpenAiProvider({ model: 'gpt-3.5-turbo', apiKey: '' }),
new MistralProvider({ model: 'open-mistral-7b', apiKey: '' }),
new OllamaProvider({ host: OLLAMA_MOCK_HOST, model: 'some-model' })
]

for (const provider of providers) {
Expand Down

0 comments on commit 14f83bf

Please sign in to comment.