All files solve-chat.ts

90% Statements 18/20
75% Branches 6/8
100% Functions 4/4
94.11% Lines 16/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144                                                                                                                                                                            2x   2x   2x   2x                       2x   1x 1x 1x                       2x       2x   2x 2x   2x   2x 2x           2x          
/**
 * Module for solving requests using the OpenAI GPT-3 API.
 * @module solveChat
 */
 
import { ChatCompletionRequestMessage } from "openai";
import { z } from "zod";
import { SolveRequestOptions, solve } from "./solve"
import { getJsonSchema } from './utils'
import { SolveJsonResponse, fullParse, handleError } from "./solve-json";
 
/**
 * Represents a system message in a conversation.
 * @interface SystemMessage
 * @property {string} role - The role of the message, which should be set to 'system'.
 * @property {any} [key] - Additional key-value pairs for custom data.
 */
export interface SystemMessage {
    role: 'system';
    [key: string]: any;
}
 
/**
 * Represents a user message in a conversation.
 * @interface UserMessage
 * @property {string} [role='user'] - The role of the message, which should be set to 'user'.
 * @property {string} message - The content of the message from the user.
 * @property {Object} data - Additional key-value pairs for custom data.
 */
export interface UserMessage {
    role: 'user';
    message: string;
    data: { [key: string]: any };
}
 
/**
 * Represents an assistant message in a conversation.
 * @interface AIMessage
 * @property {string} [role='assistant'] - The role of the message, which should be set to 'assistant'.
 * @property {string} message - The content of the message from the assistant, if any.
 * @property {Object} action - Additional key-value pairs for custom data, if any.
 */
export type AIMessage = {
    role: 'assistant';
} & ({
    message: string;
} | {
    action?: {[key: string]: any };
})
 
/**
 * Represents a conversation in the form of an array of system, user, and assistant messages.
 * @typedef {Array<SystemMessage | UserMessage | AIMessage>} Conversation
 */
export type Conversation = Array<UserMessage | AIMessage | SystemMessage>
 
/**
 * Represents a request for solving a chat-based language model.
 * @interface SolveChatRequest
 * @template Output - The expected output schema for the assistant's response.
 * @property {string} instructions - Instructions for the assistant on how to respond to the conversation.
 * @property {Conversation} messages - The conversation in the form of an array of system, user, and assistant messages.
 * @property {z.ZodType<Output>} zodSchema - The Zod schema for validating the output of the assistant's response.
 * @property {Object} custom - Additional custom data to be included in the request.
 * @property {string} [safeKey] - An optional safe key for safe content filtering.
 */
export interface SolveChatRequest<Output extends object> {
    instructions: string;
    messages: Conversation;
    zodSchema: z.ZodType<Output>;
    custom: {
        [key: string]: any
    };
    safeKey?: string;
}
 
/**
 * Solves a chat-based language model.
 * @async
 * @function solveChat
 * @param {SolveChatRequest<Output>} json - The request object for solving the chat-based language model.
 * @param {SolveRequestOptions} [options] - Additional options for the request, such as verbosity and safe key.
 * @returns {SolveChatResponse<Output>} - The response object containing the assistant's response.
 */
export async function solveChat<Output extends object>(json: SolveChatRequest<Output>, options?: SolveRequestOptions): Promise<SolveJsonResponse<Output>> {
    
    const verbose = options?.verbose || false
 
    const jsonSchema = getJsonSchema(json.zodSchema, 'Output')
 
    const messages = formatMessages(json.messages)
 
    const solved = await solve([{
            role: 'system',
            content: JSON.stringify({
                instructions: `Read the data, the conversation and use your knowledge to respond to it following this instructions and the outputSchema. ${json.instructions} \n\n Your output must be a json based on outputSchema. Your output will de parsed to JSON so do not output plain text!`,
                outputSchema: jsonSchema,
                data: { ...json.custom }
            })
        },
        ...messages
         // CORTAR CON CONTADOR DE TOKENS Y CON EMMBEDDINGS BASADOS EN LOS DOS ULTIMOS MENSAJES ??
    ], options)
    
    if (solved.status === 200) return fullParse(solved.data, json.zodSchema,verbose,json.safeKey)
 
    const data = JSON.parse(solved.data)
    Iif (verbose) console.log('SOLVE ERROR', data)
    return handleError(solved.status, data.error, data.text, verbose)
 
}
 
/**
 * Formats the conversation messages into the required format for chat completion API.
 * @function formatMessages
 * @param {Conversation} messages - The conversation in the form of an array of system, user, and assistant messages.
 * @returns {Array<ChatCompletionRequestMessage>} - The formatted messages for chat completion API.
 */
function formatMessages(messages: Conversation): Array<ChatCompletionRequestMessage> {
 
    const important = {
        important: "Dont forget to output following the outputSchema!"
    }
    
    const lastUserMessageIndex = messages.reverse().findIndex(m =>m.role === 'user')!
 
    return messages.map((message, index) => {
        const { role, ...content } = message
 
        let processedContent = {}
 
        if (index === lastUserMessageIndex) {
            processedContent = {
                ...content,
                ...important
            }
        } else EprocessedContent = { ...content }
 
        return ({
            role,
            content: JSON.stringify(processedContent)                
        }) as ChatCompletionRequestMessage
    }).reverse()
}