Package: chatlab
ChatLabChat Experiments, Simplified💬🔬ChatLab is a Python package that makes it easy to experiment with OpenAI's chat models. It provides a simple interface for chatting with the models and a way to register functions that can be called from the chat model.Best yet, it's interactive in the notebook!Notebooks to get started withLearning the BasicsRecommend and Visualize Color PalettesIntroduction to the Function RegistryCreating Knowledge Graphs with PydanticDirect Parallel Function CallingLet the Model do some Data ScienceIntroductionimportchatlabimportrandomdefflip_a_coin():'''Returns heads or tails'''returnrandom.choice(['heads','tails'])chat=chatlab.Chat()chat.register(flip_a_coin)awaitchat("Please flip a coin for me")𝑓  Ran `flip_a_coin`Input:{}Output:"tails"It landed on tails!In the notebook, text will stream into a Markdown output and function inputs and outputs are a nice collapsible display, like with ChatGPT Plugins.TODO: Include GIF/mp4 of this in actionInstallationpipinstallchatlabConfigurationYou'll need to set yourOPENAI_API_KEYenvironment variable. You can find your API key on yourOpenAI account page. I recommend setting it in an.envfile when working locally.On hosted notebook environments, set it in your Secrets to keep it safe from prying LLM eyes.What canChats enableyouto do?💬WhereChats take it next level is withChat Functions. You candeclare a functionregister the function in yourChatwatch as Chat Models call your functions!You may recall this kind of behavior from ChatGPT Plugins. Now, you can take this even further with your own custom code.As an example, let's give the large language models the ability to tell time.fromdatetimeimportdatetimefrompytzimporttimezone,all_timezones,utcfromtypingimportOptionalfrompydanticimportBaseModeldefwhat_time(tz:Optional[str]=None):'''Current time, defaulting to UTC'''iftzisNone:passeliftzinall_timezones:tz=timezone(tz)else:return'Invalid timezone'returndatetime.now(tz).strftime('%I:%M %p')classWhatTime(BaseModel):tz:Optional[str]=NoneLet's break this down.what_timeis the function we're going to provide access to. Its docstring forms thedescriptionfor the model while the schema comes from the pydanticBaseModelcalledWhatTime.importchatlabchat=chatlab.Chat()# Register our functionchat.register(what_time,WhatTime)After that, we can callchatwith direct strings (which are turned into user messages) or using simple message makers fromchatlabnameduserandsystem.awaitchat("What time is it?")𝑓  Ran `what_time`Input:{}Output:"11:19 AM"The current time is 11:19 AM.InterfaceThechatlabpackage exportsChatTheChatclass is the main way to chat using OpenAI's models. It keeps a history of your chat inChat.messages.Chat.submitsubmitis how you send all the currently built up messages over to OpenAI. Markdown output will display responses from theassistant.awaitchat.submit('What would a parent who says "I have to play zone defense" mean? ')# Markdown response inlinechat.messages[{'role':'user','content':'What does a parent of three kids mean by "I have to play zone defense"?'},{'role':'assistant','content':'Whenaparentofthreekidssays"I have to play zone defense,"itmeansthatthey...Chat.registerYou can register functions withChat.registerto make them available to the chat model. The function's docstring becomes the description of the function while the schema is derived from thepydantic.BaseModelpassed in.frompydanticimportBaseModelclassWhatTime(BaseModel):tz:Optional[str]=Nonedefwhat_time(tz:Optional[str]=None):'''Current time, defaulting to UTC'''iftzisNone:passeliftzinall_timezones:tz=timezone(tz)else:return'Invalid timezone'returndatetime.now(tz).strftime('%I:%M %p')chat.register(what_time,WhatTime)Chat.messagesThe raw messages sent and received to OpenAI. If you hit a token limit, you can remove old messages from the list to make room for more.chat.messages=chat.messages[-100:]Messaginghuman/userThese functions create a message from the user to the chat model.fromchatlabimporthumanhuman("How are you?"){"role":"user","content":"How are you?"}narrate/systemsystemmessages, also callednarrateinchatlab, allow you to steer the model in a direction. You can use these to provide context without being seen by the user. One common use is to include it as initial context for the conversation.fromchatlabimportnarratenarrate("You are a large bird"){"role":"system","content":"You are a large bird"}DevelopmentThis project uses poetry for dependency management. To get started, clone the repo and runpoetryinstall-Edev-EtestWe useruffandmypy.ContributingPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.