ronantakizawa/codereview-bench huggingface dataset:
Repository: microsoft/TypeChat
File: python/examples/multiSchema/demo.py (Python)

Code before:
        name="CoffeeShop",
        description="Order Coffee Drinks (Italian names included) and Baked Goods",
        handler=coffee_agent.handle_request,
    )

    calendar_agent = JsonPrintAgent(model=model, target_type=calendar.CalendarActions)
    router.register_agent(
        name="Calendar",
        description="Actions related to calendars, appointments, meetings, schedules",
        handler=calendar_agent.handle_request,
    )

    restaurant_agent = JsonPrintAgent(model=model, target_type=restaurant.Order)
    router.register_agent(
        name="Restaurant", description="Order pizza, beer and salads", handler=restaurant_agent.handle_request
    )

    sentiment_agent = JsonPrintAgent(model=model, target_type=sentiment.Sentiment)
    router.register_agent(
        name="Sentiment",
        description="Statements with sentiments, emotions, feelings, impressions about places, things, the surroundings",
        handler=sentiment_agent.handle_request,
    )

    # register a handler for unknown results
    router.register_agent(name="No Match", description="None of the others matched", handler=handle_unknown)

    print("🔀> ", end="", flush=True)
    for line in sys.stdin:
        await router.route_request(line)
        print("\n🔀> ", end="", flush=True)


if __name__ == "__main__":
    asyncio.run(main())


Review comment (suggestion):
```suggestion
    router.register_agent(name="No Match", description="Handles all unrecognized requests", handler=handle_unknown)

```

Code after:
        name="CoffeeShop",
        description="Order Coffee Drinks (Italian names included) and Baked Goods",
        handler=coffee_agent.handle_request,
    )

    calendar_agent = JsonPrintAgent(model=model, target_type=calendar.CalendarActions)
    router.register_agent(
        name="Calendar",
        description="Actions related to calendars, appointments, meetings, schedules",
        handler=calendar_agent.handle_request,
    )

    restaurant_agent = JsonPrintAgent(model=model, target_type=restaurant.Order)
    router.register_agent(
        name="Restaurant", description="Order pizza, beer and salads", handler=restaurant_agent.handle_request
    )

    sentiment_agent = JsonPrintAgent(model=model, target_type=sentiment.Sentiment)
    router.register_agent(
        name="Sentiment",
        description="Statements with sentiments, emotions, feelings, impressions about places, things, the surroundings",
        handler=sentiment_agent.handle_request,
    )

    # register a handler for unknown results
    router.register_agent(name="No Match", description="Handles all unrecognized requests", handler=handle_unknown)

    print("🔀> ", end="", flush=True)
    for line in sys.stdin:
        await router.route_request(line)
        print("\n🔀> ", end="", flush=True)


if __name__ == "__main__":
    asyncio.run(main())
Repository: microsoft/TypeChat
File: examples/healthData/src/main.ts (TypeScript)

Code before:
                            My health data may be complex: always record and return ALL of it.
                            Always return a response:
                            - If you don't understand what I say, ask a question.
                            - At least respond with an OK message.
                            `;

const model = createLanguageModel(process.env);
const schema = fs.readFileSync(path.join(__dirname, "healthDataSchema.ts"), "utf8");
const translator = createHealthDataTranslator<HealthDataResponse>(model, schema, "HealthDataResponse",
                        health_instructions);

// Process requests interactively or from the input file specified on the command line
processRequests("💉💊🤧> ", process.argv[2], async (request) => {
    const response = await translator.translate(request);
    if (!response.success) {
        console.log("Translation Failed ❌");
        console.log(`Context: ${response.message}`);
    }
    else {
        const healthdata = response.data;
        console.log("Translation Succeeded! ✅\n");
        console.log("JSON View");
        console.log(JSON.stringify(healthdata, undefined, 2));

        const message = healthdata["message"];
        const not_translated = healthdata["notTranslated"];

        if (message) {
            console.log(`\n📝: ${message}`);
        }
            
        if (not_translated) {
            console.log(`\n🤔: I did not understand\n ${not_translated}`)
        }
    }
});


Review comment (suggestion):
```suggestion
        const notTranslated = healthdata["notTranslated"];
```

Code after:
My health data may be complex: always record and return ALL of it.
Always return a response:
- If you don't understand what I say, ask a question.
- At least respond with an OK message.
`;

const model = createLanguageModel(process.env);
const schema = fs.readFileSync(path.join(__dirname, "healthDataSchema.ts"), "utf8");
const translator = createHealthDataTranslator<HealthDataResponse>(model, schema, "HealthDataResponse",
                        healthInstructions);

// Process requests interactively or from the input file specified on the command line
processRequests("🤧> ", process.argv[2], async (request) => {
    const response = await translator.translate(request);
    if (!response.success) {
        console.log("Translation Failed ❌");
        console.log(`Context: ${response.message}`);
    }
    else {
        const healthData = response.data;
        console.log("Translation Succeeded! ✅\n");
        console.log("JSON View");
        console.log(JSON.stringify(healthData, undefined, 2));

        const message = healthData.message;
        const notTranslated = healthData.notTranslated;

        if (message) {
            console.log(`\n📝: ${message}`);
        }
            
        if (notTranslated) {
            console.log(`\n🤔: I did not understand\n ${notTranslated}`)
        }
    }
});
Repository: microsoft/TypeChat
File: examples/healthData/src/translator.ts (TypeScript)

Code before:
import {Result, TypeChatLanguageModel, createJsonTranslator, TypeChatJsonTranslator} from "typechat";

type ChatMessage = {
    source: "system" | "user" | "assistant";
    body: string;
};

export interface TranslatorWithHistory<T extends object> {
    _chat_history: ChatMessage[];
    _max_prompt_length: number;
    _additional_agent_instructions: string;
    translator: TypeChatJsonTranslator<T>;
    translate(request: string): Promise<Result<T>>;
}

export function createHealthDataTranslator<T extends object>(model: TypeChatLanguageModel, schema: string, type_name: string, additional_agent_instructions: string): TranslatorWithHistory<T> {
    const _chat_history: ChatMessage[] = [];
    const _max_prompt_length = 2048;
    const _additional_agent_instructions = additional_agent_instructions;
    
    const translator = createJsonTranslator<T>(model, schema, type_name);
    translator.createRequestPrompt = _create_request_prompt;
    
    const customtranslator: TranslatorWithHistory<T> = {
        _chat_history,
        _max_prompt_length,
        _additional_agent_instructions,
        translator,
        translate,
    };

    return customtranslator;

    async function translate(request: string): Promise<Result<T>> {
        const response = await translator.translate(request);
        if (response.success) {
            _chat_history.push({ source: 'assistant', body: JSON.stringify(response.data) });
        }

Review comment (suggestion):
Exposing both the JSON `translator` and the `translate` method is kind of weird because if you re-assign to one, it won't affect the behavior of the other.

Code after:
import {Result, TypeChatLanguageModel, createJsonTranslator, TypeChatJsonTranslator} from "typechat";

type ChatMessage = {
    source: "system" | "user" | "assistant";
    body: object;
};

export interface TranslatorWithHistory<T extends object> {
    _chatHistory: ChatMessage[];
    _maxPromptLength: number;
    _additionalAgentInstructions: string;
    _translator: TypeChatJsonTranslator<T>;
    translate(request: string): Promise<Result<T>>;
}

export function createHealthDataTranslator<T extends object>(model: TypeChatLanguageModel, schema: string, typename: string, additionalAgentInstructions: string): TranslatorWithHistory<T> {
    const _chatHistory: ChatMessage[] = [];
    const _maxPromptLength = 2048;
    const _additionalAgentInstructions = additionalAgentInstructions;
    
    const _translator = createJsonTranslator<T>(model, schema, typename);
    _translator.createRequestPrompt = createRequestPrompt;
    
    const customtranslator: TranslatorWithHistory<T> = {
        _chatHistory,
        _maxPromptLength,
        _additionalAgentInstructions,
        _translator,
        translate,
    };

    return customtranslator;

    async function translate(request: string): Promise<Result<T>> {
        const response = await _translator.translate(request);
        if (response.success) {
            _chatHistory.push({ source: "assistant", body: response.data });
        }
Repository: microsoft/TypeChat
File: examples/healthData/src/translator.ts (TypeScript)

Code before:

        const prompt = `
        user: You are a service that translates user requests into JSON objects of type "${type_name}" according to the following TypeScript definitions:
        '''
        ${schema}
        '''

        user:
        Use precise date and times RELATIVE TO CURRENT DATE: ${now.toLocaleDateString()} CURRENT TIME: ${now.toTimeString().split(' ')[0]}
        Also turn ranges like next week and next month into precise dates
        
        user:
        ${_additional_agent_instructions}
        
        system:
        IMPORTANT CONTEXT for the user request:
        ${history_str}

        user:
        The following is a user request:
        '''
        ${intent}
        '''
        The following is the user request translated into a JSON object with 2 spaces of indentation and no properties with the value undefined:
        """
        `;
        return prompt;
    }
}


Review comment (suggestion):
Same issue as above - plus, if `schema` spans multiple lines, it won't inherit the indentation of this string.

```suggestion
user: You are a service that translates user requests into JSON objects of type "${type_name}" according to the following TypeScript definitions:
'''
${schema}
'''

user:
Use precise date and times RELATIVE TO CURRENT DATE: ${now.toLocaleDateString()} CURRENT TIME: ${now.toTimeString().split(' ')[0]}
Also turn ranges like next week and next month into precise dates

user:
${_additional_agent_instructions}

system:
IMPORTANT CONTEXT for the user request:
${history_str}

user:
The following is a user request:
'''
${intent}
'''
The following is the user request translated into a JSON object with 2 spaces of indentation and no properties with the value undefined:
"""
`;
```

Code after:
        const now = new Date();

        const prompt = `
user: You are a service that translates user requests into JSON objects of type "${typename}" according to the following TypeScript definitions:
'''
${schema}
'''

user:
Use precise date and times RELATIVE TO CURRENT DATE: ${now.toLocaleDateString()} CURRENT TIME: ${now.toTimeString().split(' ')[0]}
Also turn ranges like next week and next month into precise dates

user:
${_additionalAgentInstructions}

system:
IMPORTANT CONTEXT for the user request:
${historyStr}

user:
The following is a user request:
'''
${intent}
'''
The following is the user request translated into a JSON object with 2 spaces of indentation and no properties with the value undefined:
"""
`;
        return prompt;
    }
}
Repository: microsoft/TypeChat
File: examples/healthData/src/healthDataSchema.ts (TypeScript)

Code before:
}

// Use for health data that match nothing else. E.g. immunization, blood prssure etc
export interface OtherHealthData {
    text: string;
    when?: ApproxDatetime;
}

export interface ApproxQuantity {
    // Default: Unknown. Required
    displayText: string;
    // Optional: only if precise quantities are available
    quantity?: Quantity;
}

export interface ApproxDatetime {
    // Default: Unknown. Required
    displayText: string;
    // If precise timestamp can be set
    timestamp?: string;
}

export interface Quantity {
    // Exact number
    value: number;
    // UNITS include mg, kg, cm, pounds, liter, ml, tablet, pill, cup, per-day, per-week..ETC
    units: string;
}

Review comment (suggestion):
```suggestion
    // Units like mg, kg, cm, pounds, liter, ml, tablet, pill, cup, per-day, per-week, etc.
```

Code after:
    endDate?: ApproxDatetime;
}

// Use for health data that match nothing else. E.g. immunization, blood prssure etc
export interface OtherHealthData {
    text: string;
    when?: ApproxDatetime;
}

export interface ApproxQuantity {
    // Default: "unknown"
    displayText: string;
    // Only specify if precise quantities are available
    quantity?: Quantity;
}

export interface ApproxDatetime {
    // Default: "unknown"
    displayText: string;
    // If precise timestamp can be set
    timestamp?: string;
}

export interface Quantity {
    // Exact number
    value: number;
    // Units like mg, kg, cm, pounds, liter, ml, tablet, pill, cup, per-day, per-week, etc.
    units: string;
}
Repository: microsoft/TypeChat
File: examples/healthData/src/healthDataSchema.ts (TypeScript)

Code before:
    // E.g. 50 mg. Required
    strength: ApproxQuantity;
}

// Disease, Ailment, Injury, Sickness
export interface Condition {
    // Fix any spelling mistakes, especially phonetic spelling
    name: string;
    // When the condition started? Required
    startDate: ApproxDatetime;
    // Always ask for current status of the condition
    status: "active" | "recurrence" | "relapse" | "inactive" | "remission" | "resolved" | "unknown";
    // If the condition was no longer active
    endDate?: ApproxDatetime;
}

// Use for health data that match nothing else. E.g. immunization, blood prssure etc
export interface OtherHealthData {
    text: string;
    when?: ApproxDatetime;
}

export interface ApproxQuantity {
    // Default: Unknown. Required
    displayText: string;
    // Optional: only if precise quantities are available
    quantity?: Quantity;
}

export interface ApproxDatetime {
    // Default: Unknown. Required
    displayText: string;
    // If precise timestamp can be set
    timestamp?: string;
}

export interface Quantity {
    // Exact number
    value: number;
    // UNITS include mg, kg, cm, pounds, liter, ml, tablet, pill, cup, per-day, per-week..ETC
    units: string;
}

Review comment (suggestion):
```suggestion
    // Only specify if precise quantities are available
```

Code after:
    frequency: ApproxQuantity;
    // E.g. 50 mg. Required
    strength: ApproxQuantity;
}

// Disease, Ailment, Injury, Sickness
export interface Condition {
    // Fix any spelling mistakes, especially phonetic spelling
    name: string;
    // When the condition started.
    startDate: ApproxDatetime;
    // Always ask for current status of the condition
    status: "active" | "recurrence" | "relapse" | "inactive" | "remission" | "resolved" | "unknown";
    // If the condition was no longer active
    endDate?: ApproxDatetime;
}

// Use for health data that match nothing else. E.g. immunization, blood prssure etc
export interface OtherHealthData {
    text: string;
    when?: ApproxDatetime;
}

export interface ApproxQuantity {
    // Default: "unknown"
    displayText: string;
    // Only specify if precise quantities are available
    quantity?: Quantity;
}

export interface ApproxDatetime {
    // Default: "unknown"
    displayText: string;
    // If precise timestamp can be set
    timestamp?: string;
}

export interface Quantity {
    // Exact number
    value: number;
    // Units like mg, kg, cm, pounds, liter, ml, tablet, pill, cup, per-day, per-week, etc.
    units: string;
}
Repository: microsoft/TypeChat
File: examples/healthData/src/healthDataSchema.ts (TypeScript)

Code before:
// The following is a schema definition for enetring health data.

export interface HealthDataResponse {
    // Return this if JSON has ALL required information. Else ask questions
    data?: HealthData;
    // Use this to ask questions and give pertinent responses
    message?: string;
    // Use this parts of the user request not translateed, off topic, etc
    notTranslated?: string;
}

export interface HealthData {
    medication?: Medication[];
    condition?: Condition[];
    other?: OtherHealthData[];
}

// Meds, pills etc.
export interface Medication {
    // Fix any spelling mistakes, especially phonetic spelling
    name: string;
    // E.g. 2 tablets, 1 cup. Required
    dose: ApproxQuantity;
    // E.g. twice a day. Required
    frequency: ApproxQuantity;
    // E.g. 50 mg. Required
    strength: ApproxQuantity;
}

// Disease, Ailment, Injury, Sickness
export interface Condition {
    // Fix any spelling mistakes, especially phonetic spelling
    name: string;

Review comment (suggestion):
```suggestion
    // Use this parts of the user request not translated, off topic, etc.
```

Code after:
// The following is a schema definition for enetring health data.

export interface HealthDataResponse {
    // ONLY present when ALL required information is known.
    // Otherwise, use 'message' to keep asking questions.
    data?: HealthData;
    // Use this to ask questions and give pertinent responses
    message?: string;
    // Use this parts of the user request not translated, off topic, etc.
    notTranslated?: string;
}

export interface HealthData {
    medication?: Medication[];
    condition?: Condition[];
    other?: OtherHealthData[];
}

// Meds, pills etc.
export interface Medication {
    // Fix any spelling mistakes, especially phonetic spelling
    name: string;
    // E.g. 2 tablets, 1 cup. Required
    dose: ApproxQuantity;
    // E.g. twice a day. Required
    frequency: ApproxQuantity;
    // E.g. 50 mg. Required
    strength: ApproxQuantity;
}

// Disease, Ailment, Injury, Sickness
export interface Condition {
    // Fix any spelling mistakes, especially phonetic spelling
Repository: microsoft/TypeChat
File: examples/healthData/src/healthDataSchema.ts (TypeScript)

Code before:
// The following is a schema definition for enetring health data.

export interface HealthDataResponse {
    // Return this if JSON has ALL required information. Else ask questions
    data?: HealthData;
    // Use this to ask questions and give pertinent responses
    message?: string;
    // Use this parts of the user request not translateed, off topic, etc
    notTranslated?: string;
}

export interface HealthData {
    medication?: Medication[];
    condition?: Condition[];
    other?: OtherHealthData[];
}

// Meds, pills etc.
export interface Medication {
    // Fix any spelling mistakes, especially phonetic spelling
    name: string;
    // E.g. 2 tablets, 1 cup. Required
    dose: ApproxQuantity;
    // E.g. twice a day. Required
    frequency: ApproxQuantity;
    // E.g. 50 mg. Required
    strength: ApproxQuantity;
}


Review comment (suggestion):
```suggestion
    // ONLY present when ALL required information is known.
    // Otherwise, use 'message' to keep asking questions.
```

Code after:
// The following is a schema definition for enetring health data.

export interface HealthDataResponse {
    // ONLY present when ALL required information is known.
    // Otherwise, use 'message' to keep asking questions.
    data?: HealthData;
    // Use this to ask questions and give pertinent responses
    message?: string;
    // Use this parts of the user request not translated, off topic, etc.
    notTranslated?: string;
}

export interface HealthData {
    medication?: Medication[];
    condition?: Condition[];
    other?: OtherHealthData[];
}

// Meds, pills etc.
export interface Medication {
    // Fix any spelling mistakes, especially phonetic spelling
    name: string;
    // E.g. 2 tablets, 1 cup. Required
    dose: ApproxQuantity;
    // E.g. twice a day. Required
    frequency: ApproxQuantity;
    // E.g. 50 mg. Required
    strength: ApproxQuantity;
}
Repository: microsoft/TypeChat
File: examples/healthData/src/main.ts (TypeScript)

Code before:
My health data may be complex: always record and return ALL of it.
Always return a response:
- If you don't understand what I say, ask a question.
- At least respond with an OK message.
`;

const model = createLanguageModel(process.env);
const schema = fs.readFileSync(path.join(__dirname, "healthDataSchema.ts"), "utf8");
const translator = createHealthDataTranslator<HealthDataResponse>(model, schema, "HealthDataResponse",
                        healthInstructions);

// Process requests interactively or from the input file specified on the command line
processRequests("🤧> ", process.argv[2], async (request) => {
    const response = await translator.translate(request);
    if (!response.success) {
        console.log("Translation Failed ❌");
        console.log(`Context: ${response.message}`);
    }
    else {
        const healthdata = response.data;
        console.log("Translation Succeeded! ✅\n");
        console.log("JSON View");
        console.log(JSON.stringify(healthdata, undefined, 2));

        const message = healthdata["message"];
        const notTranslated = healthdata["notTranslated"];

        if (message) {
            console.log(`\n📝: ${message}`);
        }
            
        if (notTranslated) {
            console.log(`\n🤔: I did not understand\n ${notTranslated}`)
        }
    }
});


Review comment (suggestion):
```suggestion
        const healthData = response.data;
        console.log("Translation Succeeded! ✅\n");
        console.log("JSON View");
        console.log(JSON.stringify(healthData, undefined, 2));

        const message = healthData.message;
        const notTranslated = healthData.notTranslated;
```

Code after:
My health data may be complex: always record and return ALL of it.
Always return a response:
- If you don't understand what I say, ask a question.
- At least respond with an OK message.
`;

const model = createLanguageModel(process.env);
const schema = fs.readFileSync(path.join(__dirname, "healthDataSchema.ts"), "utf8");
const translator = createHealthDataTranslator<HealthDataResponse>(model, schema, "HealthDataResponse",
                        healthInstructions);

// Process requests interactively or from the input file specified on the command line
processRequests("🤧> ", process.argv[2], async (request) => {
    const response = await translator.translate(request);
    if (!response.success) {
        console.log("Translation Failed ❌");
        console.log(`Context: ${response.message}`);
    }
    else {
        const healthData = response.data;
        console.log("Translation Succeeded! ✅\n");
        console.log("JSON View");
        console.log(JSON.stringify(healthData, undefined, 2));

        const message = healthData.message;
        const notTranslated = healthData.notTranslated;

        if (message) {
            console.log(`\n📝: ${message}`);
        }
            
        if (notTranslated) {
            console.log(`\n🤔: I did not understand\n ${notTranslated}`)
        }
    }
});