Skip to content

feat: add request config#34

Open
liulinboyi wants to merge 1 commit intomicrosoft:mainfrom
liulinboyi:feat/addConfig
Open

feat: add request config#34
liulinboyi wants to merge 1 commit intomicrosoft:mainfrom
liulinboyi:feat/addConfig

Conversation

@liulinboyi
Copy link

@liulinboyi liulinboyi commented Jul 21, 2023

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: 'xxx@gmail.com',
            // 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;
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant