Skip to content

Commit

Permalink
Merge pull request #72 from gruz0/refactor/extract-concept-evaluator
Browse files Browse the repository at this point in the history
Extract Concept Evaluator into a separate file
  • Loading branch information
gruz0 authored Nov 6, 2024
2 parents 5706fca + 181306b commit 8eea833
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,24 @@
import OpenAI from 'openai'
import { z } from 'zod'
import { Evaluation } from '@/concept/domain/Evaluation'
import { getPromptContent } from '@/lib/prompts'

type Status = 'well-defined' | 'requires_changes' | 'not-well-defined'

interface Evaluation {
status: Status
suggestions: string[]
recommendations: string[]
painPoints: string[]
marketExistence: string
targetAudience: TargetAudience[]
}

interface TargetAudience {
segment: string
description: string
challenges: string[]
}

const ResponseSchema = z.object({
problem_evaluation: z.object({
status: z.enum(['well-defined', 'requires_changes', 'not-well-defined']),
Expand All @@ -20,7 +36,7 @@ const ResponseSchema = z.object({
}),
})

export class OpenAIService {
export class ConceptEvaluator {
private openai: OpenAI

constructor(apiKey: string) {
Expand Down Expand Up @@ -74,15 +90,13 @@ ${problem.trim()}"""`,

const analysis = ResponseSchema.parse(JSON.parse(content))

const evaluation = new Evaluation(
analysis.problem_evaluation.status,
analysis.problem_evaluation.suggestions,
analysis.problem_evaluation.recommendations,
analysis.problem_evaluation.pain_points,
analysis.problem_evaluation.market_existence.trim(),
analysis.problem_evaluation.target_audience
)

return evaluation
return {
status: analysis.problem_evaluation.status,
suggestions: analysis.problem_evaluation.suggestions,
recommendations: analysis.problem_evaluation.recommendations,
painPoints: analysis.problem_evaluation.pain_points,
marketExistence: analysis.problem_evaluation.market_existence.trim(),
targetAudience: analysis.problem_evaluation.target_audience,
}
}
}
30 changes: 28 additions & 2 deletions src/concept/events/subscribers/ConceptEvaluationSubscriber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,25 @@ import { Repository } from '@/concept/domain/Repository'
import { ConceptCreated } from '@/concept/domain/events/ConceptCreated'
import { EventHandler } from '@/concept/events/EventHandler'

type Status = 'well-defined' | 'requires_changes' | 'not-well-defined'

interface ConceptEvaluation {
status: Status
suggestions: string[]
recommendations: string[]
painPoints: string[]
marketExistence: string
targetAudience: TargetAudience[]
}

interface TargetAudience {
segment: string
description: string
challenges: string[]
}

export interface AIService {
evaluateConcept(problem: string): Promise<Evaluation>
evaluateConcept(problem: string): Promise<ConceptEvaluation>
}

// TODO: Emit event ConceptEvaluated
Expand All @@ -29,7 +46,16 @@ export class ConceptEvaluationSubscriber implements EventHandler {
await this.repository.updateConcept(
event.payload.id,
(concept): Concept => {
concept.evaluate(evaluation)
concept.evaluate(
new Evaluation(
evaluation.status,
evaluation.suggestions,
evaluation.recommendations,
evaluation.painPoints,
evaluation.marketExistence,
evaluation.targetAudience
)
)

return concept
}
Expand Down
5 changes: 2 additions & 3 deletions src/concept/service/Service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Service as IdeaService } from '@/common/client/Idea/Service'
import { ConceptRepositorySQLite } from '@/concept/adapters/ConceptRepositorySQLite'
import { EventBusInMemory } from '@/concept/adapters/EventBusInMemory'
import { OpenAIService } from '@/concept/adapters/OpenAIService'
import { ConceptEvaluator } from '@/concept/adapters/OpenAIService/ConceptEvaluator'
import { Application } from '@/concept/app/App'
import { AcceptConceptHandler } from '@/concept/app/commands/AcceptConcept'
import { EvaluateConceptHandler } from '@/concept/app/commands/EvaluateConcept'
Expand All @@ -13,12 +13,11 @@ import { env } from '@/lib/env'
const registerApp = (): Application => {
const conceptRepository = new ConceptRepositorySQLite()
const eventBus = new EventBusInMemory()
const aiService = new OpenAIService(env.OPENAI_API_KEY)
const ideaService = new IdeaService(env.IDEA_SERVICE_API_BASE)

const conceptEvaluationSubscriber = new ConceptEvaluationSubscriber(
conceptRepository,
aiService
new ConceptEvaluator(env.OPENAI_API_KEY)
)

const conceptTransitionSubscriber = new ConceptTransitionSubscriber(
Expand Down

0 comments on commit 8eea833

Please sign in to comment.