-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
208 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { ReadableStream, UnderlyingByteSource, ReadableByteStreamController } from 'stream/web' | ||
import { Ollama, ChatResponse } from 'ollama' | ||
import { AiProvider, StreamChunkCallback } from './provider' | ||
import { AiStreamEvent, encodeEvent } from './event' | ||
|
||
type OllamaStreamResponse = AsyncGenerator<ChatResponse> | ||
|
||
class OllamaByteSource implements UnderlyingByteSource { | ||
type: 'bytes' = 'bytes' | ||
response: OllamaStreamResponse | ||
chunkCallback?: StreamChunkCallback | ||
|
||
constructor (response: OllamaStreamResponse, chunkCallback?: StreamChunkCallback) { | ||
this.response = response | ||
this.chunkCallback = chunkCallback | ||
} | ||
|
||
async pull (controller: ReadableByteStreamController): Promise<void> { | ||
const { done, value } = await this.response.next() | ||
if (done !== undefined && done) { | ||
controller.close() | ||
return | ||
} | ||
|
||
let response = value.message.content | ||
if (this.chunkCallback !== undefined) { | ||
response = await this.chunkCallback(response) | ||
} | ||
|
||
const eventData: AiStreamEvent = { | ||
event: 'content', | ||
data: { | ||
response | ||
} | ||
} | ||
controller.enqueue(encodeEvent(eventData)) | ||
} | ||
} | ||
|
||
export class OllamaProvider implements AiProvider { | ||
model: string | ||
client: Ollama | ||
|
||
constructor (host: string, model: string) { | ||
this.model = model | ||
this.client = new Ollama({ host }) | ||
} | ||
|
||
async ask (prompt: string): Promise<string> { | ||
const response = await this.client.chat({ | ||
model: this.model, | ||
messages: [ | ||
{ role: 'user', content: prompt } | ||
] | ||
}) | ||
|
||
return response.message.content | ||
} | ||
|
||
async askStream (prompt: string, chunkCallback?: StreamChunkCallback | undefined): Promise<ReadableStream> { | ||
const response = await this.client.chat({ | ||
model: this.model, | ||
messages: [ | ||
{ role: 'user', content: prompt } | ||
], | ||
stream: true | ||
}) | ||
|
||
return new ReadableStream(new OllamaByteSource(response, chunkCallback)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import './api.test' | ||
import './rate-limiting.test' | ||
import './auth.test' | ||
import { mockMistralApi, mockOpenAiApi } from '../utils/mocks' | ||
import { mockMistralApi, mockOllama, mockOpenAiApi } from '../utils/mocks' | ||
|
||
mockOpenAiApi() | ||
mockMistralApi() | ||
mockOllama() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import './generator.test' | ||
import './ai-providers.test' | ||
import { mockMistralApi, mockOpenAiApi } from '../utils/mocks' | ||
import { mockMistralApi, mockOllama, mockOpenAiApi } from '../utils/mocks' | ||
|
||
mockOpenAiApi() | ||
mockMistralApi() | ||
mockOllama() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters