-
-
Notifications
You must be signed in to change notification settings - Fork 784
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
feat: ai #1117
Merged
feat: ai #1117
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
38c6f1e
feat(ai): backend api
mayneyao f1d1b5c
feat(ai): generate formula with AI
mayneyao 30f2930
chore: fix lint
mayneyao e2df8df
feat(ai): store ai config in database
mayneyao ec070be
feat(ssr): inject public config into SSR props
mayneyao e39c5c4
feat(ai): global ai config
mayneyao e32975c
fix(ai): enable AI features based on configuration
mayneyao f13518f
fix(ai): get model config from settings
mayneyao 97166f9
chore: migration
tea-artist cc4442e
Merge branch 'develop' into feat/ai
Sky-FE 699ea48
feat: adapt to multiple LLM providers
Sky-FE 4d839b8
chore: update i18n for ai config
Sky-FE b8ce3f4
perf: formula editor ai interaction
Sky-FE 33842d3
Merge branch 'develop' into feat/ai
Sky-FE 7b5aa2f
chore: update icons
Sky-FE 600f72b
perf: formula ai prompt
Sky-FE b8b46a2
feat: ai config
Sky-FE 36fd379
chore: add slider component
Sky-FE f703a0f
feat: distinguish sensitive fields of the setting api
Sky-FE 17e763c
chore: update ai icon
Sky-FE beb2d1d
chore: update i18n
Sky-FE bb08a47
feat: ai generate api
Sky-FE File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Body, Controller, Post, Res } from '@nestjs/common'; | ||
import { aiGenerateRoSchema, IAiGenerateRo } from '@teable/openapi'; | ||
import { Response } from 'express'; | ||
import { ZodValidationPipe } from '../../zod.validation.pipe'; | ||
import { TablePipe } from '../table/open-api/table.pipe'; | ||
import { AiService } from './ai.service'; | ||
|
||
@Controller('api/ai') | ||
export class AiController { | ||
constructor(private readonly aiService: AiService) {} | ||
|
||
@Post('/generate-stream') | ||
async generateStream( | ||
@Body(new ZodValidationPipe(aiGenerateRoSchema), TablePipe) aiGenerateRo: IAiGenerateRo, | ||
@Res() res: Response | ||
) { | ||
const result = await this.aiService.generateStream(aiGenerateRo); | ||
result.pipeTextStreamToResponse(res); | ||
} | ||
} |
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,12 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { SettingModule } from '../setting/setting.module'; | ||
import { AiController } from './ai.controller'; | ||
import { AiService } from './ai.service'; | ||
|
||
@Module({ | ||
imports: [SettingModule], | ||
controllers: [AiController], | ||
providers: [AiService], | ||
exports: [AiService], | ||
}) | ||
export class AiModule {} |
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,108 @@ | ||
import { createAnthropic } from '@ai-sdk/anthropic'; | ||
import { createAzure } from '@ai-sdk/azure'; | ||
import { createCohere } from '@ai-sdk/cohere'; | ||
import { createGoogleGenerativeAI } from '@ai-sdk/google'; | ||
import { createMistral } from '@ai-sdk/mistral'; | ||
import { createOpenAI } from '@ai-sdk/openai'; | ||
import { Injectable } from '@nestjs/common'; | ||
import type { IAiGenerateRo, LLMProvider } from '@teable/openapi'; | ||
import { LLMProviderType, Task } from '@teable/openapi'; | ||
import { streamText } from 'ai'; | ||
import { SettingService } from '../setting/setting.service'; | ||
import { TASK_MODEL_MAP } from './constant'; | ||
|
||
@Injectable() | ||
export class AiService { | ||
constructor(private readonly settingService: SettingService) {} | ||
|
||
readonly modelProviders = { | ||
[LLMProviderType.OPENAI]: createOpenAI, | ||
[LLMProviderType.ANTHROPIC]: createAnthropic, | ||
[LLMProviderType.GOOGLE]: createGoogleGenerativeAI, | ||
[LLMProviderType.AZURE]: createAzure, | ||
[LLMProviderType.COHERE]: createCohere, | ||
[LLMProviderType.MISTRAL]: createMistral, | ||
} as const; | ||
|
||
public parseModelKey(modelKey: string) { | ||
const [type, model, provider] = modelKey.split('@'); | ||
return { type, model, provider }; | ||
} | ||
|
||
// modelKey-> type@model@provider | ||
async getModelConfig(modelKey: string, llmProviders: LLMProvider[] = []) { | ||
const { type, model, provider } = this.parseModelKey(modelKey); | ||
|
||
const providerConfig = llmProviders.find( | ||
(p) => | ||
p.name.toLowerCase() === provider.toLowerCase() && | ||
p.type.toLowerCase() === type.toLowerCase() | ||
); | ||
|
||
if (!providerConfig) { | ||
throw new Error('AI provider configuration is not set'); | ||
} | ||
|
||
const { baseUrl, apiKey } = providerConfig; | ||
|
||
return { | ||
type, | ||
model, | ||
baseUrl, | ||
apiKey, | ||
}; | ||
} | ||
|
||
async getModelInstance( | ||
modelKey: string, | ||
llmProviders: LLMProvider[] = [] | ||
): Promise< | ||
ReturnType<ReturnType<(typeof this.modelProviders)[keyof typeof this.modelProviders]>> | ||
> { | ||
const { type, model, baseUrl, apiKey } = await this.getModelConfig(modelKey, llmProviders); | ||
|
||
if (!baseUrl || !apiKey) { | ||
throw new Error('AI configuration is not set'); | ||
} | ||
|
||
const provider = Object.entries(this.modelProviders).find(([key]) => | ||
type.toLowerCase().includes(key.toLowerCase()) | ||
)?.[1]; | ||
|
||
if (!provider) { | ||
throw new Error(`Unsupported AI provider: ${type}`); | ||
} | ||
|
||
return provider({ | ||
baseURL: baseUrl, | ||
apiKey, | ||
})(model); | ||
} | ||
|
||
async getAIConfig() { | ||
const { aiConfig } = await this.settingService.getSetting(); | ||
|
||
if (!aiConfig) { | ||
throw new Error('AI configuration is not set'); | ||
} | ||
|
||
if (!aiConfig.enable) { | ||
throw new Error('AI is not enabled'); | ||
} | ||
|
||
return aiConfig; | ||
} | ||
|
||
async generateStream(aiGenerateRo: IAiGenerateRo) { | ||
const { prompt, task = Task.Coding } = aiGenerateRo; | ||
const config = await this.getAIConfig(); | ||
const currentTaskModel = TASK_MODEL_MAP[task]; | ||
const modelKey = config[currentTaskModel as keyof typeof config] as string; | ||
const modelInstance = await this.getModelInstance(modelKey, config.llmProviders); | ||
|
||
return await streamText({ | ||
model: modelInstance, | ||
prompt: prompt, | ||
}); | ||
} | ||
} |
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,8 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
import { Task } from '@teable/openapi'; | ||
|
||
export const TASK_MODEL_MAP = { | ||
[Task.Coding]: 'codingModel', | ||
[Task.Embedding]: 'embeddingModel', | ||
[Task.Translation]: 'translationModel', | ||
}; |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TablePipe is not for this case