@@ -3,7 +3,7 @@ layout: doc-page
33title : Basic TypeScript Usage
44---
55
6- TypeChat is currently a small library, so let's take a look at some basic usage to understand it.
6+ TypeChat is currently a small library, so we can get a solid understanding just by understanding the following example:
77
88``` ts
99import fs from " fs" ;
@@ -34,6 +34,8 @@ processRequests("😀> ", process.argv[2], async (request) => {
3434});
3535```
3636
37+ Let's break it down step-by-step.
38+
3739## Providing a Model
3840
3941TypeChat can be used with any language model.
@@ -77,19 +79,25 @@ For even more convenience, TypeChat also provides a function to infer whether yo
7779export function createLanguageModel(env : Record <string , string | undefined >): TypeChatLanguageModel
7880```
7981
80- You can populate your environment variables , and based on whether ` OPENAI_API_KEY ` or ` AZURE_OPENAI_API_KEY ` is set , you ' ll get a model of the appropriate type.
82+ With ` createLanguageModel ` , you can populate your environment variables and pass them in .
83+ Based on whether ` OPENAI_API_KEY ` or ` AZURE_OPENAI_API_KEY ` is set , you ' ll get a model of the appropriate type.
84+
85+ Regardless , of how you decide to construct your model , it is important to avoid committing credentials directly in source .
86+ One way to make this work between production and development environments is to use a ` .env ` file in development , and specify that ` .env ` in your ` .gitignore ` .
87+ You can use a library like [` dotenv ` ](https :// www.npmjs.com/package/dotenv) to help load these up.
8188
8289` ` ` ts
8390import dotenv from "dotenv";
8491dotenv.config(/*...*/);
92+
93+ // ...
94+
8595import * as typechat from "typechat";
8696const model = typechat.createLanguageModel(process.env);
8797` ` `
8898
89- Regardless , of how you decide to construct your model , we recommend keeping your secret tokens / API keys in a ` .env ` file , and specifying ` .env ` in a ` .gitignore ` .
90- You can use a library like [` dotenv ` ](https :// www.npmjs.com/package/dotenv) to help load these up.
9199
92- ## Loading the Schema
100+ ## Defining and Loading the Schema
93101
94102TypeChat describes types to language models to help guide their responses .
95103In this case , we are using a ` TypeScriptJsonValidator ` which uses the TypeScript compiler to validate data against a set of types .
@@ -243,9 +251,9 @@ A `TypeChatJsonTranslator` brings these together.
243251import { createJsonTranslator } from " typechat" ;
244252` ` `
245253
246- A translator takes both a model and a validator, and provides a way to translate some user input into objects within our schema.
254+ A translator takes both a model and a validator, and provides a way to translate some user input into objects following our schema.
247255To do so, it crafts a prompt based on the schema, reaches out to the model, parses out JSON data, and attempts validation.
248- Optionally, it will craft repair prompts and retry if validation failed. .
256+ Optionally, it will craft repair prompts and retry if validation fails .
249257
250258` ` ` ts
251259const translator = createJsonTranslator (model , validator );
0 commit comments