TypeChat icon indicating copy to clipboard operation
TypeChat copied to clipboard

feat: add request config

Open liulinboyi opened this issue 2 years ago • 0 comments

Add request config eg: For Proxy request:

examples\calendar\src\main.ts

import fs from "fs";
import path from "path";
import dotenv from "dotenv";
import { createLanguageModel, createJsonTranslator, processRequests } from "typechat";
import { CalendarActions } from './calendarActionsSchema';
import { SocksProxyAgent } from 'socks-proxy-agent' // 7.0.0

// TODO: use local .env file.
dotenv.config({ path: path.join(__dirname, "../../../.env") });

const PROXY_HOST = "127.0.0.1";
const PROXY_PORT = "1080";
const model = createLanguageModel(process.env, 
    {
        httpsAgent: new SocksProxyAgent({
            hostname: PROXY_HOST,
            // userId: '[email protected]',
            // password: 'xxxxx'
            host: PROXY_HOST,
            port: PROXY_PORT,
            rejectUnauthorized: false,
        } as any)
    }
);
const schema = fs.readFileSync(path.join(__dirname, "calendarActionsSchema.ts"), "utf8");
const translator = createJsonTranslator<CalendarActions>(model, schema, "CalendarActions");
translator.validator.stripNulls = true;

// 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(response.message);
        return;
    }
    const calendarActions = response.data;
    console.log(JSON.stringify(calendarActions, undefined, 2));
    if (calendarActions.actions.some(item => item.actionType === "unknown")) {
        console.log("I didn't understand the following:");
        for (const action of calendarActions.actions) {
            if (action.actionType === "unknown") console.log(action.text);
        }
        return;
    }
});

liulinboyi avatar Jul 21 '23 06:07 liulinboyi