Taming Language Models through TypeScript

Rate this content
Bookmark

You've probably played with bots like ChatGPT and used them to brainstorm. Maybe you've noticed that getting responses in the right structure or format has been a challenge. As people, we're okay with that - but programs are much more finicky. We've been experimenting with using TypeScript itself as a way of guiding and validating responses from language models.

FAQ

TypeChat is a library developed by the TypeScript team at Microsoft designed to facilitate the integration of language models into applications. It leverages TypeScript types to guide language models in producing structured JSON responses that conform to specified types. This allows for more precise and easily parsable responses from AI models, enhancing application functionality and user interactions.

Yes, TypeChat can handle complex data schemas. It uses TypeScript types to define the expected format and structure of the JSON responses from language models. This capability enables TypeChat to manage various object types and properties, ensuring that the responses are well-structured and adhere to the defined schemas.

Using TypeScript types with language models in TypeChat provides a clear structure for the AI's responses, minimizes parsing errors, and simplifies data validation. TypeScript's static typing system helps ensure that the data conforms to the specified schema, reducing errors and improving reliability in application development.

TypeChat ensures the accuracy of AI-generated responses through a validation process that uses TypeScript. If the AI's response does not match the expected TypeScript types, TypeChat can prompt the AI to retry generating the response. This iterative validation process helps in refining the responses to align closely with the required data formats.

Yes, TypeChat is customizable for specific application needs. Developers can define their own TypeScript types that describe the exact format and structure of the data they expect from the language model. This flexibility allows TypeChat to be adapted for various use cases and applications, ranging from simple data retrieval tasks to more complex interactions.

Developers can find more information and examples on using TypeChat in the TypeChat GitHub repository. The repository includes a readme with detailed examples ranging from basic to advanced usage, providing a comprehensive guide for integrating TypeChat into their projects.

Daniel Rosenwasser
Daniel Rosenwasser
26 min
21 Sep, 2023

Comments

Sign in or register to post your comment.

Video Summary and Transcription

TypeChat is an open-source library that uses TypeScript types to guide and validate responses from language models. It allows for the creation of complex responses and provides a way to repair errors in the model. TypeChat programs enable better flow of data and the ability to refer to individual steps. The math example demonstrates the use of a program translator and metaprogramming techniques for type safety. Language models trained on both code and prose perform well in this context.

1. Introduction to TypeChat

Short description:

Hi everyone, my name is Daniel Rosenwasser and I'm the program manager on the TypeScript team at Microsoft. Today, I want to talk to you about TypeChat, another thing I've been working on recently with others on my team. In the last few months, you've probably seen a lot about artificial intelligence and large language models. These language models are very powerful, and you may have had the idea of trying to bring the smarts of one of these language models into your app, maybe to add a natural language interface in some way.

Hi everyone, my name is Daniel Rosenwasser and I'm the program manager on the TypeScript team at Microsoft. Today, I want to talk to you about TypeChat, another thing I've been working on recently with others on my team.

So, in the last few months, you've probably seen a lot about artificial intelligence and large language models. These language models are very powerful, and you've probably gotten the chance to even use them in the form of something like ChatGBT or a similar sort of chat program where you can ask the model questions, it can give you answers, you can iterate through ideas, and it's just a great way to creatively iterate through concepts and whatnot.

That works really well for people, but these language models are very powerful, and you may have had the idea of trying to bring the smarts of one of these language models into your app, maybe to add a natural language interface in some way. So for an example, let's say we have some sort of app that's supposed to help us plan out plans for a day, plans for the weekend, maybe specific to a location. It doesn't seem impractical given the fact that these models have been trained on so much data in the world, so they might know a lot about Seattle or some other city that you're trying to scout out ideas for.

2. Using TypeScript types to guide language models

Short description:

In this app, you want to get data from language models. However, language models often produce data that is not easy to parse. You can get language models to respond in JSON, but it may not always match the expected format. TypeScript types can guide the language model and provide the desired format. However, the language model may still produce responses that don't conform exactly. Validation is needed to push the AI to try again.

In this app, maybe we just want to be able to ask a question, that's our user intent, and then get a set of results with a couple of venues and each of their descriptions as well. So that seems all good, but how would you go about trying to get one of these language models to produce data that you can use in this app?

The thing that you may have realized in trying to do something like this is you end up trying to often pamper the AI to give it in a specific format, and even once it's in that format, it comes up with natural language that's just not always going to be easy to parse. So for example, here's a prompt and response that took a couple of tries to get even reasonable, right? One of the things that you might notice is I end up trying to just give it a little bit of guidance on how I expect the answer to be, right?

This isn't so bad, right? But mostly because it's come up with a regular answer in a specific format. And so it's actually given me a format of list items. Each of them is numbered. And then between the venue and the description, I have a colon. Now, is the AI always going to come back with that schema or format? Not necessarily. A lot of these language models are non-deterministic. So you really can't count on this format. But even if you could count on that format, you can't always trust that the data is going to be uniformly parsable, right?

So for example, in this case, I have a list and I have everything in this format. What about trying to split the colon, right? You might try to say, let me just try to shave each of the numbers off and then split by the colon. But what if one of the items in your list has a colon inside of the title or something like that, right? You're basically trying to do natural language parsing at this point, right? And now you have a bug, and now you have to figure out how to be resilient against that. And so this ends up being a little bit, impractical for most people, right? It is very hard to parse natural language.

But many of you probably also realize that you can get the language models to respond in the form of JSON. And that's great, right? Now you actually have something that your app can easily just do a JSON.parse or whatever, and get the data that you need and work off of that. But that only really works for sort of simple examples, right? Here I was able to say, here's an example of the JSON that I want to get back, right? I have a venue and a description, a venue and a description. And the AI is pretty good at figuring that out. But it doesn't tell us about maybe optional properties, maybe the case where you have three different kinds of objects that you expect to be in a specific position, things like that. And so just giving examples would be impractical because you would get into these combinatorial explosions of all the types of things that you would want to actually provide. So examples aren't enough. What you need is something a little bit more.

And it turns out there is a format that does work out pretty well, for the most part, in our experience. And it's something that you're all familiar with here at this TypeScript conference, which is types. TypeScript types are actually a great format for describing the exact format that we want out of a language model and all the sorts of shapes that we're expecting. So types are actually really good at guiding the language model into the thing that we want, right? And you can actually take the types in an application, like the actual text of the types in your program, take a user intent, and craft a prompt that you can send into an AI service, into a language model. And that will provide you with JSON, You would say, give me a response in the form of JSON, here's the type that you should conform to when you provide that response. And so, now you're able to guide the language model.

But like I said, the language model isn't always going to come back with a response that is actually exactly as we expect, right? So maybe it comes back with JSON, but it's not of the format precisely, right? So we can always say, hey, you didn't get me correct in the JSON, try again. But you need something else that pushes an AI so that you can say try again as well. And that's the validation.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

TypeScript and React: Secrets of a Happy Marriage
React Advanced Conference 2022React Advanced Conference 2022
21 min
TypeScript and React: Secrets of a Happy Marriage
Top Content
TypeScript and React are inseparable. What's the secret to their successful union? Quite a lot of surprisingly strange code. Learn why useRef always feels weird, how to wrangle generics in custom hooks, and how union types can transform your components.
A Framework for Managing Technical Debt
TechLead Conference 2023TechLead Conference 2023
35 min
A Framework for Managing Technical Debt
Top Content
Let’s face it: technical debt is inevitable and rewriting your code every 6 months is not an option. Refactoring is a complex topic that doesn't have a one-size-fits-all solution. Frontend applications are particularly sensitive because of frequent requirements and user flows changes. New abstractions, updated patterns and cleaning up those old functions - it all sounds great on paper, but it often fails in practice: todos accumulate, tickets end up rotting in the backlog and legacy code crops up in every corner of your codebase. So a process of continuous refactoring is the only weapon you have against tech debt.In the past three years, I’ve been exploring different strategies and processes for refactoring code. In this talk I will describe the key components of a framework for tackling refactoring and I will share some of the learnings accumulated along the way. Hopefully, this will help you in your quest of improving the code quality of your codebases.

Debugging JS
React Summit 2023React Summit 2023
24 min
Debugging JS
Top Content
As developers, we spend much of our time debugging apps - often code we didn't even write. Sadly, few developers have ever been taught how to approach debugging - it's something most of us learn through painful experience.  The good news is you _can_ learn how to debug effectively, and there's several key techniques and tools you can use for debugging JS and React apps.
React's Most Useful Types
React Day Berlin 2023React Day Berlin 2023
21 min
React's Most Useful Types
Top Content
We don't think of React as shipping its own types. But React's types are a core part of the framework - overseen by the React team, and co-ordinated with React's major releases.In this live coding talk, we'll look at all the types you've been missing out on. How do you get the props type from a component? How do you know what ref a component takes? Should you use React.FC? And what's the deal with JSX.Element?You'll walk away with a bunch of exciting ideas to take to your React applications, and hopefully a new appreciation for the wonders of React and TypeScript working together.
Building a Voice-Enabled AI Assistant With Javascript
JSNation 2023JSNation 2023
21 min
Building a Voice-Enabled AI Assistant With Javascript
Top Content
In this talk, we'll build our own Jarvis using Web APIs and langchain. There will be live coding.
Power Fixing React Performance Woes
React Advanced Conference 2023React Advanced Conference 2023
22 min
Power Fixing React Performance Woes
Top Content
Next.js and other wrapping React frameworks provide great power in building larger applications. But with great power comes great performance responsibility - and if you don’t pay attention, it’s easy to add multiple seconds of loading penalty on all of your pages. Eek! Let’s walk through a case study of how a few hours of performance debugging improved both load and parse times for the Centered app by several hundred percent each. We’ll learn not just why those performance problems happen, but how to diagnose and fix them. Hooray, performance! ⚡️

Workshops on related topic

React, TypeScript, and TDD
React Advanced Conference 2021React Advanced Conference 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
AI on Demand: Serverless AI
DevOps.js Conf 2024DevOps.js Conf 2024
163 min
AI on Demand: Serverless AI
Top Content
Featured WorkshopFree
Nathan Disidore
Nathan Disidore
In this workshop, we discuss the merits of serverless architecture and how it can be applied to the AI space. We'll explore options around building serverless RAG applications for a more lambda-esque approach to AI. Next, we'll get hands on and build a sample CRUD app that allows you to store information and query it using an LLM with Workers AI, Vectorize, D1, and Cloudflare Workers.
Best Practices and Advanced TypeScript Tips for React Developers
React Advanced Conference 2022React Advanced Conference 2022
148 min
Best Practices and Advanced TypeScript Tips for React Developers
Top Content
Featured Workshop
Maurice de Beijer
Maurice de Beijer
Are you a React developer trying to get the most benefits from TypeScript? Then this is the workshop for you.In this interactive workshop, we will start at the basics and examine the pros and cons of different ways you can declare React components using TypeScript. After that we will move to more advanced concepts where we will go beyond the strict setting of TypeScript. You will learn when to use types like any, unknown and never. We will explore the use of type predicates, guards and exhaustive checking. You will learn about the built-in mapped types as well as how to create your own new type map utilities. And we will start programming in the TypeScript type system using conditional types and type inferring.
Working With OpenAI and Prompt Engineering for React Developers
React Advanced Conference 2023React Advanced Conference 2023
98 min
Working With OpenAI and Prompt Engineering for React Developers
Top Content
Workshop
Richard Moss
Richard Moss
In this workshop we'll take a tour of applied AI from the perspective of front end developers, zooming in on the emerging best practices when it comes to working with LLMs to build great products. This workshop is based on learnings from working with the OpenAI API from its debut last November to build out a working MVP which became PowerModeAI (A customer facing ideation and slide creation tool).
In the workshop they'll be a mix of presentation and hands on exercises to cover topics including:
- GPT fundamentals- Pitfalls of LLMs- Prompt engineering best practices and techniques- Using the playground effectively- Installing and configuring the OpenAI SDK- Approaches to working with the API and prompt management- Implementing the API to build an AI powered customer facing application- Fine tuning and embeddings- Emerging best practice on LLMOps
Deep TypeScript Tips & Tricks
Node Congress 2024Node Congress 2024
83 min
Deep TypeScript Tips & Tricks
Top Content
Workshop
Josh Goldberg
Josh Goldberg
TypeScript has a powerful type system with all sorts of fancy features for representing wild and wacky JavaScript states. But the syntax to do so isn't always straightforward, and the error messages aren't always precise in telling you what's wrong. Let's dive into how many of TypeScript's more powerful features really work, what kinds of real-world problems they solve, and how to wrestle the type system into submission so you can write truly excellent TypeScript code.
Building a Shopify App with React & Node
React Summit Remote Edition 2021React Summit Remote Edition 2021
87 min
Building a Shopify App with React & Node
Top Content
WorkshopFree
Jennifer Gray
Hanna Chen
2 authors
Shopify merchants have a diverse set of needs, and developers have a unique opportunity to meet those needs building apps. Building an app can be tough work but Shopify has created a set of tools and resources to help you build out a seamless app experience as quickly as possible. Get hands on experience building an embedded Shopify app using the Shopify App CLI, Polaris and Shopify App Bridge.We’ll show you how to create an app that accesses information from a development store and can run in your local environment.