You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.7 KiB

11 months ago
  1. const AIbitat = require("../../agents/aibitat");
  2. /**
  3. * Execute an LLM instruction flow step
  4. * @param {Object} config Flow step configuration
  5. * @param {{introspect: Function, variables: Object, logger: Function}} context Execution context with introspect function
  6. * @returns {Promise<string>} Processed result
  7. */
  8. async function executeLLMInstruction(config, context) {
  9. const { instruction, inputVariable, resultVariable } = config;
  10. const { introspect, variables, logger } = context;
  11. introspect(`Processing data with LLM instruction...`);
  12. if (!variables[inputVariable]) {
  13. logger(`Input variable ${inputVariable} not found`);
  14. throw new Error(`Input variable ${inputVariable} not found`);
  15. }
  16. try {
  17. introspect(`Sending request to LLM...`);
  18. // Ensure the input is a string since we are sending it to the LLM direct as a message
  19. let input = variables[inputVariable];
  20. if (typeof input === "object") input = JSON.stringify(input);
  21. if (typeof input !== "string") input = String(input);
  22. const aibitat = new AIbitat();
  23. const provider = aibitat.getProviderForConfig(aibitat.defaultProvider);
  24. const completion = await provider.complete([
  25. {
  26. role: "system",
  27. content: `Follow these instructions carefully: ${instruction}`,
  28. },
  29. {
  30. role: "user",
  31. content: input,
  32. },
  33. ]);
  34. introspect(`Successfully received LLM response`);
  35. if (resultVariable) config.resultVariable = resultVariable;
  36. return completion.result;
  37. } catch (error) {
  38. logger(`LLM processing failed: ${error.message}`, error);
  39. throw new Error(`LLM processing failed: ${error.message}`);
  40. }
  41. }
  42. module.exports = executeLLMInstruction;