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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "TypeChat in 5 Minutes" doc #65

Merged
merged 3 commits into from
Apr 16, 2024
Merged

Add "TypeChat in 5 Minutes" doc #65

merged 3 commits into from
Apr 16, 2024

Conversation

DanielRosenwasser
Copy link
Member

I'm not sure if this is the appropriate title - but I think this sort of doc would be helpful for users who are looking for a breakdown of our sentiment example.

Some TODOs:

  • Add the "here's how it looks when put together"
  • Make sure it all can be copy-pasted together
  • Decide on the right title if not this


## Providing a Model

TypeChat is mostly model agnostic.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does "mostly" mean here?

site/src/docs/typechat-in-5-minutes.md Outdated Show resolved Hide resolved
```

A translator takes a model, the schema file contents, and the name of the type within the schema file.
That type name effectively acts as the "entry-point" of our schema for the language model.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be worth being explicit that your schema can (obviously) contain multiple types, but you must specify the "entry" type.

site/src/docs/typechat-in-5-minutes.md Outdated Show resolved Hide resolved
site/src/docs/typechat-in-5-minutes.md Outdated Show resolved Hide resolved
@weykon
Copy link
Contributor

weykon commented Jul 28, 2023

Looking forward to this PR

@bennycode
Copy link

Thank you @DanielRosenwasser for offering this handy guide on using TypeChat. I was very excited when I first learnt about TypeChat and quickly went through the examples to integrate it with my existing code.

During the process, I encountered some challenges when using TypeChat within an ESM module. I'd like to share my findings, as they might be helpful in preventing difficulties for others.

I tried to load the schema file but noticed that you cannot simply target it with __dirname:

// Load up the schema file contents.
const schema = fs.readFileSync(path.join(__dirname, "sentimentSchema.ts"), "utf8");

After executing the code, I got the following error:

ReferenceError: __dirname is not defined in ES module scope

I was able to solve this problem by mimicking the behavior of __dirname and __filename using import.meta:

import fs from 'node:fs';
import path from 'node:path';
import {fileURLToPath} from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const texts = path.join(__dirname, 'texts.txt');
const schemaFile = path.join(__dirname, 'sentimentSchema.ts');

// Load up the schema file contents.
const schema = fs.readFileSync(schemaFile, 'utf8');

I encountered another issue when trying to import the schema using the following method:

// Load up the type from our schema.
import type { SentimentResponse } from "./sentimentSchema";

I received a CustomError because Node.js couldn't locate the module, even though I was running my code using ts-node-esm:

CustomError: Cannot find module

After doing some research, I found that file extensions are mandatory when using ES Modules to resolve relative specifiers. Since .ts extensions are only supported with allowImportingTsExtensions, I made the decision to use .js extensions (even though I was referring to TS code), and it did the trick for me:

// Load up the type from our schema.
import type { SentimentResponse } from "./sentimentSchema.js";

The rest of the description worked perfectly, and I'm very happy about what you and the team are creating here. I'm so happy that I even created a TypeChat video tutorial, and it's not longer than 5 minutes. So, "TypeChat in 5 Minutes" is a reality! 😊

Great job! 👍

Sending my best wishes from Berlin!
Benny

@DanielRosenwasser
Copy link
Member Author

Thanks @bennycode, this is great feedback - I hadn't yet gotten the chance to work on ESM examples, and I knew that there would be some rough patches with __dirname. It'd be ideal for all of our example code to just work across ESM and CJS, but import.meta isn't available outside of modules.

@hjonasson
Copy link

I read through the entire doc and learned a lot! Can/will this PR be completed?

@DanielRosenwasser DanielRosenwasser merged commit e52f5a2 into main Apr 16, 2024
8 checks passed
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.

7 participants