Skip to content

Commit ce16ea9

Browse files
authored
Merge pull request #117 from TECH-C-LT/feat/sunshine-service
Sunshine Service の作成
2 parents 8cc7d09 + 0d55055 commit ce16ea9

File tree

5 files changed

+69
-8
lines changed

5 files changed

+69
-8
lines changed

apps/api/src/features/guardians/guardian.test.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

apps/api/src/features/sunshines/sunshine.controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { SunshineTextDTO } from '@peace-net/shared/types/sunshine'
22
import { handleError } from '@peace-net/shared/utils/error-handler'
33
import { Context } from 'hono'
44

5-
import { ISunshineUseCase } from './sunshine.usecase'
5+
import { ISunshineUseCase } from '~/features/sunshines/sunshine.usecase'
66

77
export class SunshineController {
88
constructor(private sunshineUseCase: ISunshineUseCase) {}

apps/api/src/features/sunshines/sunshine.route.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ import { sunshineTextRequestSchema } from '@peace-net/shared/schemas/sunshine'
33
import { handleError } from '@peace-net/shared/utils/error-handler'
44
import { Hono } from 'hono'
55

6-
import { SunshineController } from './sunshine.controller'
7-
import { SunshineUseCase } from './sunshine.usecase'
6+
import { getEnv } from '~/config/environment'
7+
import { SunshineController } from '~/features/sunshines/sunshine.controller'
8+
import { SunshineService } from '~/features/sunshines/sunshine.service'
9+
import { SunshineUseCase } from '~/features/sunshines/sunshine.usecase'
10+
import { OpenAIClient } from '~/libs/openai'
811

912
/**
1013
* Sunshine APIのルーティングを定義します
@@ -22,7 +25,11 @@ sunshineRoutes.post(
2225
return
2326
}),
2427
async (c) => {
25-
return new SunshineController(new SunshineUseCase()).sunshineText(c)
28+
return new SunshineController(
29+
new SunshineUseCase(
30+
new SunshineService(OpenAIClient(getEnv(c).OPENAI_API_KEY)),
31+
),
32+
).sunshineText(c)
2633
},
2734
)
2835

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import { OpenAIProvider } from '@ai-sdk/openai'
2+
import { SunshineResult } from '@peace-net/shared/types/sunshine'
3+
import { generateObject } from 'ai'
4+
import { z } from 'zod'
5+
6+
const systemPrompt = `
7+
あなたは、ネガティブな表現をポジティブで建設的な表現に変換する専門家です。以下の指針に従ってテキストを変換してください:
8+
9+
1. 入力されたテキストの感情的なトーンを分析し、ネガティブな要素を特定してください。
10+
11+
2. ネガティブな表現を、同じ意味を持つポジティブな表現に置き換えてください。例えば:
12+
- 「失敗した」→「改善の機会を得た」
13+
- 「問題がある」→「課題がある」
14+
- 「できない」→「まだ習得途中である」
15+
16+
3. 批判的な表現を建設的なフィードバックに変換してください。
17+
18+
4. 悲観的な見方を、現実的でありながら希望的な見方に変えてください。
19+
20+
5. 攻撃的な言葉遣いを、協調的で理解を示す表現に変更してください。
21+
22+
6. 元のメッセージの核心や意図を維持しつつ、より前向きな表現方法を見つけてください。
23+
24+
7. 文化的な配慮を行い、適切かつ敬意を払った表現を使用してください。
25+
26+
8. 変換後のテキストが自然で流暢に読めるようにしてください。
27+
28+
9. 極端な美化や現実逃避にならないよう注意し、建設的で現実的なポジティブさを目指してください。
29+
30+
入力されたテキストを上記の指針に従って変換し、より前向きで建設的な表現にしてください。
31+
32+
変換したテキストは、'text'オブジェクトに格納してください。
33+
`
34+
export interface ISunshineService {
35+
sunshineText(text: string): Promise<SunshineResult>
36+
}
37+
38+
export class SunshineService implements ISunshineService {
39+
constructor(private openai: OpenAIProvider) {}
40+
41+
async sunshineText(text: string): Promise<SunshineResult> {
42+
const { object } = await generateObject({
43+
model: this.openai('gpt-4o-mini'),
44+
schema: z.object({ text: z.string() }),
45+
messages: [
46+
{ role: 'system', content: systemPrompt },
47+
{ role: 'user', content: `変換するテキスト: ${text}` },
48+
],
49+
})
50+
51+
return object
52+
}
53+
}

apps/api/src/features/sunshines/sunshine.usecase.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,21 @@ import {
44
} from '@peace-net/shared/types/sunshine'
55
import { failure, success, type Result } from '@peace-net/shared/utils/result'
66

7+
import { ISunshineService } from '~/features/sunshines/sunshine.service'
8+
79
export interface ISunshineUseCase {
810
sunshineText(input: SunshineTextInput): Promise<Result<SunshineResult>>
911
}
1012

1113
export class SunshineUseCase implements ISunshineUseCase {
12-
constructor() {}
14+
constructor(private SunshineService: ISunshineService) {}
1315

1416
async sunshineText(
1517
input: SunshineTextInput,
1618
): Promise<Result<SunshineResult>> {
1719
try {
1820
const { text } = input
19-
// TODO serviceを追加する
21+
const result = await this.SunshineService.sunshineText(text)
2022

2123
// TODO: increment user plans total requests used
2224
// await this.userPlanService.incrementTotalRequestsUsed(userId)
@@ -26,7 +28,7 @@ export class SunshineUseCase implements ISunshineUseCase {
2628
// await this.usageLogService.incrementUsageLog(apiKeyId, 'guardians')
2729

2830
return success({
29-
text: 'Sunshine: ' + text,
31+
text: result.text,
3032
})
3133
} catch (error) {
3234
return failure(error)

0 commit comments

Comments
 (0)