Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add request config #34

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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.

None yet

1 participant