From ddceebde53d4926b2ab69dff1cd6c40a2c594db2 Mon Sep 17 00:00:00 2001 From: bsorrentino Date: Sun, 17 Mar 2024 16:39:49 +0100 Subject: [PATCH] refactor: move update plantuml method in AgentExecutor module --- AIAgent/Sources/AIAgent/AgentExecutor.swift | 35 ++++++++++++++++++++- PlantUML/OpenAIObservableService.swift | 28 ++++------------- 2 files changed, 40 insertions(+), 23 deletions(-) diff --git a/AIAgent/Sources/AIAgent/AgentExecutor.swift b/AIAgent/Sources/AIAgent/AgentExecutor.swift index 986eea4..0a87736 100644 --- a/AIAgent/Sources/AIAgent/AgentExecutor.swift +++ b/AIAgent/Sources/AIAgent/AgentExecutor.swift @@ -263,7 +263,9 @@ func routeDiagramTranslation( state: AgentExecutorState ) async throws -> String /* @objc optional */ func progress(_ message: String) -> Void } -public func agentExecutor( openAI: OpenAI, imageUrl: String, delegate:T ) async throws -> String? { +public func runTranslateDrawingToPlantUML( openAI: OpenAI, + imageUrl: String, + delegate:T ) async throws -> String? { let workflow = GraphState { AgentExecutorState() } @@ -302,3 +304,34 @@ public func agentExecutor( openAI: OpenAI, imageUrl: St } +public func updatePlantUML( openAI: OpenAI, + withModel model: Model, + input: String, + withInstruction instruction: String ) async throws -> String? { + let query = ChatQuery( + model: model, + messages: [ + .init(role: .system, content: + """ + You are my plantUML assistant. + You must answer exclusively with diagram syntax. + """), + .init( role: .assistant, content: input ), + .init( role: .user, content: instruction ) + ], + temperature: 0.0, + topP: 1.0 + ) + + let chat = try await openAI.chats(query: query) + + let result = chat.choices[0].message.content + + if case .string(let content) = result { + + return content + } + + return nil + +} diff --git a/PlantUML/OpenAIObservableService.swift b/PlantUML/OpenAIObservableService.swift index 5f869f1..576e8c3 100644 --- a/PlantUML/OpenAIObservableService.swift +++ b/PlantUML/OpenAIObservableService.swift @@ -97,7 +97,7 @@ class OpenAIObservableService : ObservableObject { } @MainActor - func query( input: String, instruction: String ) async -> String? { + func updatePlantUMLDiagram( input: String, instruction: String ) async -> String? { guard let openAI /*, let openAIModel */, case .Ready = status else { return nil @@ -107,26 +107,10 @@ class OpenAIObservableService : ObservableObject { do { - let query = ChatQuery( - model: openAIModel, - messages: [ - .init(role: .system, content: - """ - You are my plantUML assistant. - You must answer exclusively with diagram syntax. - """), - .init( role: .assistant, content: input ), - .init( role: .user, content: instruction ) - ], - temperature: 0.0, - topP: 1.0 - ) - - let chat = try await openAI.chats(query: query) - - let result = chat.choices[0].message.content - - if case .string(let content) = result { + if let content = try await updatePlantUML(openAI: openAI, + withModel: openAIModel, + input: input, + withInstruction: instruction) { status = .Ready @@ -167,7 +151,7 @@ extension OpenAIObservableService { do { - if let content = try await agentExecutor( openAI: openAI, imageUrl: imageUrl, delegate:delegate) { + if let content = try await runTranslateDrawingToPlantUML( openAI: openAI, imageUrl: imageUrl, delegate:delegate) { status = .Ready