Skip to content
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

Sunshine APIにAIのモデルを選択できるオプションを追加 #195

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions apps/api/src/features/guardians/guardian.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ import { UsageLogService } from '~/features/usageLogs/usageLog.service'
import { UsageFacade } from '~/features/usages/usage.facade'
import { UserPlanRepository } from '~/features/userPlans/userPlan.repository'
import { UserPlanService } from '~/features/userPlans/userPlan.service'
import { AnthropicClient } from '~/libs/anthropic'
import { GoogleClient } from '~/libs/google'
import { OpenAIClient } from '~/libs/openai'
import { AnthropicClient, GoogleClient, OpenAIClient } from '~/libs/models'
import { SupabaseClient } from '~/libs/supabase'

/**
Expand Down
26 changes: 10 additions & 16 deletions apps/api/src/features/guardians/guardian.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import { GoogleGenerativeAIProvider } from '@ai-sdk/google'
import { OpenAIProvider } from '@ai-sdk/openai'
import { InternalServerError } from '@peace-net/shared/core/error'
import { categoryScoresSchema } from '@peace-net/shared/schemas/guardian'
import type { CategoryScores, Models } from '@peace-net/shared/types/guardian'
import type { CategoryScores } from '@peace-net/shared/types/guardian'
import { Models } from '@peace-net/shared/types/model'
import { generateObject } from 'ai'
import { z } from 'zod'

import { selectAIModel } from '~/libs/models'

const systemPrompt = `
# 役割
あなたは日本語のコンテンツモデレーションの専門家です。
Expand Down Expand Up @@ -85,21 +88,12 @@ export class GuardianService implements IGuardianService {
selectedModel: Models,
): Promise<CategoryScores> {
try {
let model
switch (selectedModel) {
case 'gpt-4o-mini':
model = this.openai('gpt-4o-mini')
break
case 'claude-3-haiku':
model = this.anthropic('claude-3-haiku-20240307')
break
case 'gemini-1.5-flash':
model = this.google('models/gemini-1.5-flash')
break
default:
model = this.openai('gpt-4o-mini')
break
}
const model = selectAIModel(
selectedModel,
this.openai,
this.anthropic,
this.google,
)

const { object } = await generateObject({
model,
Expand Down
8 changes: 6 additions & 2 deletions apps/api/src/features/sunshines/sunshine.route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { UsageLogService } from '~/features/usageLogs/usageLog.service'
import { UsageFacade } from '~/features/usages/usage.facade'
import { UserPlanRepository } from '~/features/userPlans/userPlan.repository'
import { UserPlanService } from '~/features/userPlans/userPlan.service'
import { OpenAIClient } from '~/libs/openai'
import { AnthropicClient, GoogleClient, OpenAIClient } from '~/libs/models'
import { SupabaseClient } from '~/libs/supabase'

/**
Expand All @@ -33,7 +33,11 @@ sunshineRoutes.post(
async (c) => {
return new SunshineController(
new SunshineUseCase(
new SunshineService(OpenAIClient(getEnv(c).OPENAI_API_KEY)),
new SunshineService(
OpenAIClient(getEnv(c).OPENAI_API_KEY),
AnthropicClient(getEnv(c).ANTHROPIC_API_KEY),
GoogleClient(getEnv(c).GOOGLE_API_KEY),
),
new UsageFacade(
new UserPlanService(
new UserPlanRepository(
Expand Down
29 changes: 24 additions & 5 deletions apps/api/src/features/sunshines/sunshine.service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
import { AnthropicProvider } from '@ai-sdk/anthropic'
import { GoogleGenerativeAIProvider } from '@ai-sdk/google'
import { OpenAIProvider } from '@ai-sdk/openai'
import { InternalServerError } from '@peace-net/shared/core/error'
import { Models } from '@peace-net/shared/types/model'
import { SunshineResult } from '@peace-net/shared/types/sunshine'
import { generateObject } from 'ai'
import { z } from 'zod'

import { selectAIModel } from '~/libs/models'

const systemPrompt = `
あなたは、ネガティブな表現をポジティブで建設的な表現に変換する専門家です。以下の指針に従ってテキストを変換してください:

Expand Down Expand Up @@ -33,16 +38,30 @@ const systemPrompt = `
変換したテキストは、'text'オブジェクトに格納してください。
`
export interface ISunshineService {
sunshineText(text: string): Promise<SunshineResult>
sunshineText(text: string, selectedModel: Models): Promise<SunshineResult>
}

export class SunshineService implements ISunshineService {
constructor(private openai: OpenAIProvider) {}
constructor(
private openai: OpenAIProvider,
private anthropic: AnthropicProvider,
private google: GoogleGenerativeAIProvider,
) {}

async sunshineText(text: string): Promise<SunshineResult> {
async sunshineText(
text: string,
selectedModel: Models,
): Promise<SunshineResult> {
try {
const model = selectAIModel(
selectedModel,
this.openai,
this.anthropic,
this.google,
)

const { object } = await generateObject({
model: this.openai('gpt-4o-mini'),
model,
schema: z.object({ text: z.string() }),
messages: [
{ role: 'system', content: systemPrompt },
Expand All @@ -53,7 +72,7 @@ export class SunshineService implements ISunshineService {
return object
} catch (error) {
console.error(error)
throw new InternalServerError('Failed to generate object')
throw new InternalServerError('Failed to generate text')
}
}
}
4 changes: 2 additions & 2 deletions apps/api/src/features/sunshines/sunshine.usecase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ export class SunshineUseCase implements ISunshineUseCase {
input: SunshineTextInput,
): Promise<Result<SunshineResult>> {
try {
const { text, userId, apiKeyId } = input
const result = await this.SunshineService.sunshineText(text)
const { text, model, userId, apiKeyId } = input
const result = await this.SunshineService.sunshineText(text, model)

await this.usageFacade.incrementUsage(userId, apiKeyId, 'sunshines')

Expand Down
13 changes: 0 additions & 13 deletions apps/api/src/libs/anthropic.ts

This file was deleted.

13 changes: 0 additions & 13 deletions apps/api/src/libs/google.ts

This file was deleted.

62 changes: 62 additions & 0 deletions apps/api/src/libs/models.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { AnthropicProvider, createAnthropic } from '@ai-sdk/anthropic'
import {
createGoogleGenerativeAI,
GoogleGenerativeAIProvider,
} from '@ai-sdk/google'
import { createOpenAI, OpenAIProvider } from '@ai-sdk/openai'
import { Models } from '@peace-net/shared/types/model'
import { LanguageModel } from 'ai'

/**
* OpenAI APIのクライアントを生成します
*
* @param apiKey - OpenAI APIのAPIキー
* @returns OpenAI APIのクライアント
*/
export const OpenAIClient = (apiKey: string) => {
return createOpenAI({
apiKey,
})
}

/**
* Google Generative AI APIのクライアントを生成します
*
* @param apiKey - Google Generative AI APIのAPIキー
* @returns Google Generative AIのクライアント
*/
export const GoogleClient = (apiKey: string) => {
return createGoogleGenerativeAI({
apiKey,
})
}

/**
* Anthropic APIのクライアントを生成します
*
* @param apiKey - Anthropic APIのAPIキー
* @returns Anthropic APIのクライアント
*/
export const AnthropicClient = (apiKey: string) => {
return createAnthropic({
apiKey,
})
}

export const selectAIModel: (
selectedModel: Models,
openai: OpenAIProvider,
anthropic: AnthropicProvider,
google: GoogleGenerativeAIProvider,
) => LanguageModel = (selectedModel, openai, anthropic, google) => {
switch (selectedModel) {
case 'gpt-4o-mini':
return openai('gpt-4o-mini')
case 'claude-3-haiku':
return anthropic('claude-3-haiku-20240307')
case 'gemini-1.5-flash':
return google('models/gemini-1.5-flash')
default:
return openai('gpt-4o-mini')
}
}
13 changes: 0 additions & 13 deletions apps/api/src/libs/openai.ts

This file was deleted.

7 changes: 1 addition & 6 deletions packages/shared/src/schemas/guardian.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { z } from 'zod'
import { modelsSchema } from './model'

export const categoryScoresSchema = z.object({
sexual: z.number(),
Expand All @@ -8,12 +9,6 @@ export const categoryScoresSchema = z.object({
defamation: z.number(),
})

export const modelsSchema = z.union([
z.literal('gpt-4o-mini'),
z.literal('claude-3-haiku'),
z.literal('gemini-1.5-flash'),
])

export const guardianTextRequestSchema = z.object({
text: z.string().max(500),
score_threshold: z.number().max(1).min(0.1).optional().default(0.5),
Expand Down
7 changes: 7 additions & 0 deletions packages/shared/src/schemas/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from 'zod'

export const modelsSchema = z.union([
z.literal('gpt-4o-mini'),
z.literal('claude-3-haiku'),
z.literal('gemini-1.5-flash'),
])
2 changes: 2 additions & 0 deletions packages/shared/src/schemas/sunshine.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { z } from 'zod'
import { modelsSchema } from './model'

export const sunshineTextRequestSchema = z.object({
text: z.string().max(500),
model: modelsSchema.optional().default('gpt-4o-mini'),
})

export const sunshineResultSchema = z.object({
Expand Down
3 changes: 0 additions & 3 deletions packages/shared/src/types/guardian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ import {
guardianTextRequestSchema,
guardianImageRequestSchema,
guardianResultSchema,
modelsSchema,
} from '../schemas/guardian'

export type Category = keyof z.infer<typeof categoryScoresSchema>

export type CategoryScores = z.infer<typeof categoryScoresSchema>

export type Models = z.infer<typeof modelsSchema>

export type GuardianTextDTO = z.infer<typeof guardianTextRequestSchema>

export type GuardianTextInput = GuardianTextDTO & {
Expand Down
4 changes: 4 additions & 0 deletions packages/shared/src/types/model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { z } from 'zod'
import { modelsSchema } from '../schemas/model'

export type Models = z.infer<typeof modelsSchema>
Loading