Skip to content

Commit

Permalink
Refactored brain code into brain
Browse files Browse the repository at this point in the history
  • Loading branch information
geir-eilertsen committed Dec 21, 2024
1 parent eb18d0b commit 803cf60
Show file tree
Hide file tree
Showing 10 changed files with 246 additions and 155 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.assetvisor.marvin.brain.springai.adapters;

import com.assetvisor.marvin.robot.domain.brain.AsleepException;
import com.assetvisor.marvin.robot.domain.brain.BrainResponder;
import com.assetvisor.marvin.robot.domain.brain.ForInvokingBrain;
import com.assetvisor.marvin.robot.domain.brain.Brain;
import com.assetvisor.marvin.robot.domain.brain.ForInvokingIntelligence;
import com.assetvisor.marvin.toolkit.memory.ForRemembering;
import com.assetvisor.marvin.robot.domain.environment.EnvironmentDescription;
import com.assetvisor.marvin.robot.domain.tools.Tool;
Expand All @@ -26,7 +26,7 @@
import org.springframework.stereotype.Component;

@Component
public class BrainSpringAiAdapter implements ForInvokingBrain, ForRemembering {
public class BrainSpringAiAdapter implements ForInvokingIntelligence, ForRemembering {

private final Log LOG = LogFactory.getLog(BrainSpringAiAdapter.class);
@SuppressWarnings("FieldCanBeLocal")
Expand Down Expand Up @@ -94,7 +94,7 @@ private Document map(EnvironmentDescription environmentDescription) {
}

@Override
public void invoke(String message, boolean reply, BrainResponder responder, String conversationId) {
public void invoke(String message, boolean reply, Brain brain, String conversationId) {
if (chatClient == null) {
throw new AsleepException("Brain is asleep, please wake it up first.");
}
Expand All @@ -110,7 +110,7 @@ public void invoke(String message, boolean reply, BrainResponder responder, Stri
assert chatResponse != null;
String responseString = chatResponse.getResult().getOutput().getContent();
if (reply) {
responder.respond(responseString, conversationId);
brain.respond(responseString, conversationId);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package com.assetvisor.marvin.robot.application.services;

import com.assetvisor.marvin.robot.application.SomethingWasSaidUseCase;
import com.assetvisor.marvin.robot.application.SomethingHappenedInTheEnvironmentUseCase;
import com.assetvisor.marvin.robot.application.SomethingWasSaidUseCase;
import com.assetvisor.marvin.robot.application.SomethingWasTextedUseCase;
import com.assetvisor.marvin.robot.domain.brain.BrainResponder;
import com.assetvisor.marvin.robot.domain.brain.ForInvokingBrain;
import com.assetvisor.marvin.robot.domain.communication.ConversationMessage;
import com.assetvisor.marvin.robot.domain.brain.Brain;
import com.assetvisor.marvin.robot.domain.brain.ForInvokingIntelligence;
import com.assetvisor.marvin.robot.domain.communication.ForCheckingIfAnybodyIsListening;
import com.assetvisor.marvin.robot.domain.communication.ForConvertingSpeechToText;
import com.assetvisor.marvin.robot.domain.communication.ForConvertingTextToSpeech;
import com.assetvisor.marvin.robot.domain.communication.ForTexting;
import com.assetvisor.marvin.robot.domain.communication.SpeechMessage;
import com.assetvisor.marvin.robot.domain.communication.SpeechBuffer;
import com.assetvisor.marvin.robot.domain.communication.SpeechMessage;
import com.assetvisor.marvin.robot.domain.communication.TextMessage;
import com.assetvisor.marvin.robot.domain.environment.Observation;
import jakarta.annotation.Resource;
Expand All @@ -28,7 +27,7 @@ public class InteractionService implements
Log LOG = LogFactory.getLog(getClass());

@Resource
private ForInvokingBrain forInvokingBrain;
private ForInvokingIntelligence forInvokingIntelligence;
@Resource
private ForTexting forTexting;
@Resource
Expand All @@ -42,55 +41,27 @@ public class InteractionService implements

@Override
public void observe(Observation observation) {
LOG.info(observation);
forInvokingBrain.invoke(
observation.toString(),
true,
new BrainResponder(
forTexting,
forConvertingTextToSpeech,
forCheckingIfAnybodyIsListening,
speechBuffer
),
ConversationMessage.DEFAULT_CONVERSATION_ID
);
brain().observe(observation);
}

@Override
public void read(TextMessage message, boolean echoToSender) {
LOG.info(message);
if(echoToSender) {
forTexting.text(message, true);
}
forInvokingBrain.invoke(
message.getContent(),
true,
new BrainResponder(
forTexting,
forConvertingTextToSpeech,
forCheckingIfAnybodyIsListening,
speechBuffer
),
message.conversationId()
);
brain().read(message, echoToSender);
}

@Override
public void listenTo(SpeechMessage speech) {
String text = forConvertingSpeechToText.convert(speech.getAudio());
LOG.info(text);
forTexting.text(new TextMessage(speech.getSender(), speech.conversationId(), text), true);
forInvokingBrain.invoke(
text,
true,
new BrainResponder(
forTexting,
forConvertingTextToSpeech,
forCheckingIfAnybodyIsListening,
speechBuffer
),
speech.conversationId()
);
brain().listenTo(speech);
}

private Brain brain() {
return new Brain(
forTexting,
forConvertingTextToSpeech,
forConvertingSpeechToText,
forCheckingIfAnybodyIsListening,
speechBuffer,
forInvokingIntelligence
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.assetvisor.marvin.robot.application.InitialiseUseCase;
import com.assetvisor.marvin.robot.domain.brain.ForForgettingEverything;
import com.assetvisor.marvin.robot.domain.brain.ForInvokingBrain;
import com.assetvisor.marvin.robot.domain.brain.ForInvokingIntelligence;
import com.assetvisor.marvin.robot.domain.brain.Teacher;
import com.assetvisor.marvin.robot.domain.environment.ForGettingEnvironmentDescriptions;
import com.assetvisor.marvin.robot.domain.environment.ForPersistingEnvironmentDescriptions;
Expand All @@ -23,7 +23,7 @@ public class LifecycleService implements InitialiseUseCase {
@Resource
private ForForgettingEverything forForgettingEverything;
@Resource
private ForInvokingBrain forInvokingBrain;
private ForInvokingIntelligence forInvokingIntelligence;
@Resource
private ForPersistingRobotDescription forPersistingRobotDescription;
@Resource
Expand All @@ -41,7 +41,7 @@ public void initialise() {
forForgettingEverything.forgetEverything();

Teacher teacher = new Teacher(
forInvokingBrain,
forInvokingIntelligence,
forPersistingEnvironmentDescriptions,
forGettingEnvironmentDescriptions
);
Expand All @@ -54,7 +54,7 @@ public void wakeUp() {
forGettingEnvironmentTools,
forGettingOwnTools
);
forInvokingBrain.wakeUp(
forInvokingIntelligence.wakeUp(
forPersistingRobotDescription.read(),
toolCollector.all()
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package com.assetvisor.marvin.robot.domain.brain;

import com.assetvisor.marvin.robot.domain.communication.ConversationMessage;
import com.assetvisor.marvin.robot.domain.communication.ForCheckingIfAnybodyIsListening;
import com.assetvisor.marvin.robot.domain.communication.ForConvertingSpeechToText;
import com.assetvisor.marvin.robot.domain.communication.ForConvertingTextToSpeech;
import com.assetvisor.marvin.robot.domain.communication.ForTexting;
import com.assetvisor.marvin.robot.domain.communication.SpeechMessage;
import com.assetvisor.marvin.robot.domain.communication.SpeechBuffer;
import com.assetvisor.marvin.robot.domain.communication.TextMessage;
import com.assetvisor.marvin.robot.domain.environment.Observation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class Brain {

private static final Log LOG = LogFactory.getLog(Brain.class);

private final ForTexting forTexting;
private final ForConvertingTextToSpeech forConvertingTextToSpeech;
private final ForConvertingSpeechToText forConvertingSpeechToText;
private final ForCheckingIfAnybodyIsListening forCheckingIfAnybodyIsListening;
private final SpeechBuffer speechBuffer;
private final ForInvokingIntelligence forInvokingIntelligence;

public Brain(ForTexting forTexting,
ForConvertingTextToSpeech forConvertingTextToSpeech,
ForConvertingSpeechToText forConvertingSpeechToText,
ForCheckingIfAnybodyIsListening forCheckingIfAnybodyIsListening,
SpeechBuffer speechBuffer,
ForInvokingIntelligence forInvokingIntelligence
) {
this.forTexting = forTexting;
this.forConvertingTextToSpeech = forConvertingTextToSpeech;
this.forConvertingSpeechToText = forConvertingSpeechToText;
this.forCheckingIfAnybodyIsListening = forCheckingIfAnybodyIsListening;
this.speechBuffer = speechBuffer;
this.forInvokingIntelligence = forInvokingIntelligence;
}

public void observe(Observation observation) {
LOG.info(observation);
forInvokingIntelligence.invoke(
observation.toString(),
true,
this,
ConversationMessage.DEFAULT_CONVERSATION_ID
);
}

public void read(TextMessage message, boolean echoToSender) {
LOG.info(message);
if(echoToSender) {
forTexting.text(message, true);
}
forInvokingIntelligence.invoke(
message.getContent(),
true,
this,
message.conversationId()
);
}

public void listenTo(SpeechMessage speech) {
String text = forConvertingSpeechToText.convert(speech.getAudio());
LOG.info(text);
forTexting.text(
new TextMessage(
speech.getSender(),
speech.conversationId(),
text),
true
);
forInvokingIntelligence.invoke(
text,
true,
this,
speech.conversationId()
);
}

public void respond(String message, String conversationId) {
message(message, conversationId);
if(forCheckingIfAnybodyIsListening.isAnybodyListening()) {
speak(message, conversationId);
}
}

private void speak(String message, String conversationId) {
byte[] speech = forConvertingTextToSpeech.convert(message);
speechBuffer.add(new SpeechMessage("Marvin", conversationId, speech));
}

private void message(String message, String conversationId) {
forTexting.text(new TextMessage("Marvin", conversationId, message), false);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
import com.assetvisor.marvin.robot.domain.jobdescription.RobotDescription;
import java.util.List;

public interface ForInvokingBrain {
public interface ForInvokingIntelligence {
void teach(
List<EnvironmentDescription> environmentDescriptions
);
void wakeUp(
RobotDescription robotDescription,
List<Tool<?,?>> environmentFunctions
);
void invoke(String message, boolean reply, BrainResponder responder, String conversationId);
void invoke(String message, boolean reply, Brain brain, String conversationId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public class Teacher {

private final Log LOG = LogFactory.getLog(getClass());

private final ForInvokingBrain forInvokingBrain;
private final ForInvokingIntelligence forInvokingIntelligence;
private final ForPersistingEnvironmentDescriptions forPersistingEnvironmentDescriptions;
private final ForGettingEnvironmentDescriptions forGettingEnvironmentDescriptions;

public Teacher(
ForInvokingBrain forInvokingBrain,
ForInvokingIntelligence forInvokingIntelligence,
ForPersistingEnvironmentDescriptions forPersistingEnvironmentDescriptions,
ForGettingEnvironmentDescriptions forGettingEnvironmentDescriptions
) {
this.forInvokingBrain = forInvokingBrain;
this.forInvokingIntelligence = forInvokingIntelligence;
this.forPersistingEnvironmentDescriptions = forPersistingEnvironmentDescriptions;
this.forGettingEnvironmentDescriptions = forGettingEnvironmentDescriptions;
}
Expand All @@ -30,7 +30,7 @@ public void teach() {
LOG.info("Teaching brain about environment...");
List<EnvironmentDescription> environmentDescriptions = new ArrayList<>(forPersistingEnvironmentDescriptions.load());
environmentDescriptions.addAll(forGettingEnvironmentDescriptions.getEnvironmentDescriptions());
forInvokingBrain.teach(
forInvokingIntelligence.teach(
environmentDescriptions
);
LOG.info("Teaching done.");
Expand Down
Loading

0 comments on commit 803cf60

Please sign in to comment.