-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from taichimaeda/add-support-for-other-providers
Add support for other providers
- Loading branch information
Showing
78 changed files
with
4,074 additions
and
789 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
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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { APIClient, ChatMessage } from '..'; | ||
|
||
// TODO: | ||
// Implement API client for Gemini. | ||
|
||
export class GeminiAPIClient implements APIClient { | ||
fetchChat(messages: ChatMessage[]): AsyncGenerator<string | undefined> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
fetchCompletions( | ||
prefix: string, | ||
suffix: string, | ||
): Promise<string | undefined> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
testConnection(): Promise<boolean> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} |
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,46 @@ | ||
import { Notice } from 'obsidian'; | ||
import OpenAI from 'openai'; | ||
import Markpilot from 'src/main'; | ||
import { validateURL } from 'src/utils'; | ||
import { APIClient } from '..'; | ||
import { PromptGenerator } from '../prompts/generator'; | ||
import { Provider } from '../providers'; | ||
import { CostsTracker } from '../providers/costs'; | ||
import { OpenAICompatibleAPIClient } from './openai-compatible'; | ||
|
||
export class OllamaAPIClient | ||
extends OpenAICompatibleAPIClient | ||
implements APIClient | ||
{ | ||
constructor( | ||
generator: PromptGenerator, | ||
tracker: CostsTracker, | ||
plugin: Markpilot, | ||
) { | ||
super(generator, tracker, plugin); | ||
} | ||
|
||
get provider(): Provider { | ||
return 'ollama'; | ||
} | ||
|
||
get openai(): OpenAI | undefined { | ||
const { settings } = this.plugin; | ||
|
||
const apiUrl = settings.providers.ollama.apiUrl; | ||
if (apiUrl === undefined) { | ||
new Notice('Ollama API URL is not set.'); | ||
return; | ||
} | ||
if (!validateURL(apiUrl)) { | ||
new Notice('Ollama API URL is invalid.'); | ||
return; | ||
} | ||
|
||
return new OpenAI({ | ||
baseURL: apiUrl, | ||
apiKey: 'ollama', // Required but ignored. | ||
dangerouslyAllowBrowser: true, | ||
}); | ||
} | ||
} |
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,136 @@ | ||
import { getEncoding } from 'js-tiktoken'; | ||
import { Notice } from 'obsidian'; | ||
import OpenAI from 'openai'; | ||
import Markpilot from 'src/main'; | ||
import { APIClient, ChatMessage } from '..'; | ||
import { PromptGenerator } from '../prompts/generator'; | ||
import { Provider } from '../providers'; | ||
import { CostsTracker } from '../providers/costs'; | ||
import { DEFAULT_MODELS } from '../providers/models'; | ||
|
||
export abstract class OpenAICompatibleAPIClient implements APIClient { | ||
constructor( | ||
protected generator: PromptGenerator, | ||
protected tracker: CostsTracker, | ||
protected plugin: Markpilot, | ||
) {} | ||
|
||
abstract get provider(): Provider; | ||
|
||
abstract get openai(): OpenAI | undefined; | ||
|
||
async *fetchChat(messages: ChatMessage[]) { | ||
if (this.openai === undefined) { | ||
return; | ||
} | ||
|
||
const { settings } = this.plugin; | ||
try { | ||
const stream = await this.openai.chat.completions.create({ | ||
messages, | ||
model: settings.chat.model, | ||
max_tokens: settings.chat.maxTokens, | ||
temperature: settings.chat.temperature, | ||
top_p: 1, | ||
n: 1, | ||
stream: true, | ||
}); | ||
|
||
const contents = []; | ||
for await (const chunk of stream) { | ||
const content = chunk.choices[0].delta.content ?? ''; | ||
contents.push(content); | ||
yield content; | ||
} | ||
|
||
// Update usage cost estimates. | ||
const enc = getEncoding('gpt2'); // Assume GPT-2 encoding | ||
const inputMessage = messages | ||
.map((message) => message.content) | ||
.join('\n'); | ||
const outputMessage = contents.join(''); | ||
const inputTokens = enc.encode(inputMessage).length; | ||
const outputTokens = enc.encode(outputMessage).length; | ||
await this.tracker.add( | ||
settings.chat.provider, | ||
settings.chat.model, | ||
inputTokens, | ||
outputTokens, | ||
); | ||
} catch (error) { | ||
console.error(error); | ||
new Notice( | ||
'Failed to fetch chat completions. Make sure your API key or API URL is correct.', | ||
); | ||
} | ||
} | ||
|
||
async fetchCompletions(prefix: string, suffix: string) { | ||
if (this.openai === undefined) { | ||
return; | ||
} | ||
|
||
const { settings } = this.plugin; | ||
try { | ||
const messages = this.generator.generate(prefix, suffix); | ||
const completions = await this.openai.chat.completions.create({ | ||
messages, | ||
model: settings.completions.model, | ||
max_tokens: settings.completions.maxTokens, | ||
temperature: settings.completions.temperature, | ||
top_p: 1, | ||
n: 1, | ||
stop: ['\n\n\n'], | ||
}); | ||
|
||
// Update usage cost estimates. | ||
const inputTokens = completions.usage?.prompt_tokens ?? 0; | ||
const outputTokens = completions.usage?.completion_tokens ?? 0; | ||
await this.tracker.add( | ||
settings.completions.provider, | ||
settings.completions.model, | ||
inputTokens, | ||
outputTokens, | ||
); | ||
|
||
const content = completions.choices[0].message.content; | ||
if (content === null) { | ||
return; | ||
} | ||
return this.generator.parse(content); | ||
} catch (error) { | ||
console.error(error); | ||
console.log(JSON.stringify(error)); | ||
new Notice( | ||
'Failed to fetch completions. Make sure your API key or API URL is correct.', | ||
); | ||
} | ||
} | ||
|
||
async testConnection() { | ||
if (this.openai === undefined) { | ||
return false; | ||
} | ||
|
||
try { | ||
const response = await this.openai.chat.completions.create({ | ||
messages: [ | ||
{ | ||
role: 'user', | ||
content: 'Say this is a test', | ||
}, | ||
], | ||
model: DEFAULT_MODELS[this.provider], | ||
max_tokens: 1, | ||
temperature: 0, | ||
top_p: 1, | ||
n: 1, | ||
}); | ||
|
||
return response.choices[0].message.content !== ''; | ||
} catch (error) { | ||
console.error(error); | ||
return false; | ||
} | ||
} | ||
} |
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,44 @@ | ||
import { Notice } from 'obsidian'; | ||
import OpenAI from 'openai'; | ||
import Markpilot from 'src/main'; | ||
import { APIClient } from '..'; | ||
import { PromptGenerator } from '../prompts/generator'; | ||
import { Provider } from '../providers'; | ||
import { CostsTracker } from '../providers/costs'; | ||
import { OpenAICompatibleAPIClient } from './openai-compatible'; | ||
|
||
export class OpenAIAPIClient | ||
extends OpenAICompatibleAPIClient | ||
implements APIClient | ||
{ | ||
constructor( | ||
generator: PromptGenerator, | ||
tracker: CostsTracker, | ||
plugin: Markpilot, | ||
) { | ||
super(generator, tracker, plugin); | ||
} | ||
|
||
get provider(): Provider { | ||
return 'openai'; | ||
} | ||
|
||
get openai(): OpenAI | undefined { | ||
const { settings } = this.plugin; | ||
|
||
const apiKey = settings.providers.openai.apiKey; | ||
if (apiKey === undefined) { | ||
new Notice('OpenAI API key is not set.'); | ||
return; | ||
} | ||
if (!apiKey.startsWith('sk')) { | ||
new Notice('OpenAI API key is invalid.'); | ||
return; | ||
} | ||
|
||
return new OpenAI({ | ||
apiKey, | ||
dangerouslyAllowBrowser: true, | ||
}); | ||
} | ||
} |
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,45 @@ | ||
import { Notice } from 'obsidian'; | ||
import OpenAI from 'openai'; | ||
import Markpilot from 'src/main'; | ||
import { APIClient } from '..'; | ||
import { PromptGenerator } from '../prompts/generator'; | ||
import { Provider } from '../providers'; | ||
import { CostsTracker } from '../providers/costs'; | ||
import { OpenAICompatibleAPIClient } from './openai-compatible'; | ||
|
||
export class OpenRouterAPIClient | ||
extends OpenAICompatibleAPIClient | ||
implements APIClient | ||
{ | ||
constructor( | ||
generator: PromptGenerator, | ||
tracker: CostsTracker, | ||
plugin: Markpilot, | ||
) { | ||
super(generator, tracker, plugin); | ||
} | ||
|
||
get provider(): Provider { | ||
return 'openrouter'; | ||
} | ||
|
||
get openai(): OpenAI | undefined { | ||
const { settings } = this.plugin; | ||
|
||
const apiKey = settings.providers.openrouter.apiKey; | ||
if (apiKey === undefined) { | ||
new Notice('OpenRouter API key is not set.'); | ||
return; | ||
} | ||
if (!apiKey.startsWith('sk')) { | ||
new Notice('OpenRouter API key is invalid.'); | ||
return; | ||
} | ||
|
||
return new OpenAI({ | ||
apiKey, | ||
baseURL: 'https://openrouter.ai/api/v1', | ||
dangerouslyAllowBrowser: true, | ||
}); | ||
} | ||
} |
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,17 @@ | ||
export interface APIClient { | ||
fetchChat(messages: ChatMessage[]): AsyncGenerator<string | undefined>; | ||
fetchCompletions(prefix: string, suffix: string): Promise<string | undefined>; | ||
testConnection(): Promise<boolean>; | ||
} | ||
|
||
export type ChatRole = 'system' | 'assistant' | 'user'; | ||
|
||
export interface ChatMessage { | ||
role: ChatRole; | ||
content: string; | ||
} | ||
|
||
export interface ChatHistory { | ||
messages: ChatMessage[]; | ||
response: string; | ||
} |
Oops, something went wrong.