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 Python usage docs #237

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Add Python usage docs #237

wants to merge 2 commits into from

Conversation

DanielRosenwasser
Copy link
Member

No description provided.

@DanielRosenwasser DanielRosenwasser changed the title Switch sentiment example to be a dataclass. Add Python usage docs Apr 18, 2024
Copy link
Contributor

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

Here's a very nitpicky review of the basic Python docs (sorry :-).

import schema as sentiment
from typechat import Failure, TypeChatJsonTranslator, TypeChatValidator, create_language_model, process_requests

async def main():
Copy link
Contributor

Choose a reason for hiding this comment

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

Stray line.

`complete` is just a function that takes a `string` and eventually returns a `string` if all goes well.

For convenience, TypeChat provides two functions out of the box to connect to the OpenAI API and Azure's OpenAI Services.
You can call these directly.
Copy link
Contributor

Choose a reason for hiding this comment

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

Honestly that's all you need to understand the example; the earlier part of this section (from line 44 on) is advanced stuff that I'd move much further down (maybe with a link from here).

from typechat import Failure, TypeChatJsonTranslator, TypeChatValidator, create_language_model, process_requests

async def main():
env_vals = dotenv_values()
Copy link
Contributor

Choose a reason for hiding this comment

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

This requires a supporting import, a hint on how to install it (there are many modules with dotenv in their name on PyPI, but what we need is pip install python-dotenv), and a brief explanation of which keys create_language_model() looks for. I stumbled quite a bit over this. :-(

Copy link
Contributor

Choose a reason for hiding this comment

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

I realize that you discuss this further down -- I wonder if there's a way to present things so that it's easier to just read it from top to bottom.

Comment on lines +88 to +89
With `create_language_model`, you can populate your environment variables and pass them in.
Based on whether `OPENAI_API_KEY` or `AZURE_OPENAI_API_KEY` is set, you'll get a model of the appropriate type.
Copy link
Contributor

Choose a reason for hiding this comment

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

It's honestly a bit confusing to call these "environment variables" since they are read from a file, not stored in the OS- (or at least shell-) managed environment variables. Unless the default (vals=None) actually reads os.environ?

With `create_language_model`, you can populate your environment variables and pass them in.
Based on whether `OPENAI_API_KEY` or `AZURE_OPENAI_API_KEY` is set, you'll get a model of the appropriate type.

The `TypeChatLanguageModel` returned by these functions has a few attributes you might find useful:
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
The `TypeChatLanguageModel` returned by these functions has a few attributes you might find useful:
The `TypeChatLanguageModel` returned by these functions has a few writable attributes you might find useful:

`process_requests` takes 3 things.
First, there's the prompt prefix - this is what a user will see before their own text in interactive scenarios.
You can make this playful.
We like to use emoji here. 😄
Copy link
Contributor

Choose a reason for hiding this comment

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

Why? I still haven't figured out how to type emoji on a keyboard -- I only know how to do it on my phone. :-(


We'll come back to this.

## Creating the Prompt
Copy link
Contributor

Choose a reason for hiding this comment

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

"Prompt" is an ambiguous term here. Does it refer to the user input prompt (only used when file_path is None) or the prompt for the LLM?

Comment on lines +206 to +207
file_path = sys.argv[1] if len(sys.argv) == 2 else None
await process_requests("😀> ", file_path, request_handler)
Copy link
Contributor

Choose a reason for hiding this comment

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

In Python it's more common to name such a variable or argument filename or file.

```

We're calling the `translate` method on each string and getting a response.
If something goes wrong, TypeChat will retry requests up to a maximum specified by `retry_max_attempts` on our `model`.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a feature of the Model class, right? May be helpful to mention that.

A `TypeChatJsonTranslator` brings all these concepts together.
A translator takes a language model, a validator, and our expected type, and provides a way to translate some user input into objects following our schema.
To do so, it crafts a prompt based on the schema, reaches out to the model, parses out JSON data, and attempts validation.
Optionally, it will craft repair prompts and retry if validation fails.
Copy link
Contributor

Choose a reason for hiding this comment

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

I would love to read more about the repair process. I expect that in practice one might have to tweak this.

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

2 participants