{"id":13643535,"url":"https://github.com/rgbkrk/chatlab","last_synced_at":"2025-04-06T02:12:51.704Z","repository":{"id":176265959,"uuid":"624629841","full_name":"rgbkrk/chatlab","owner":"rgbkrk","description":"⚡️🧪 Fast LLM Tool Calling Experimentation, big and smol","archived":false,"fork":false,"pushed_at":"2024-09-25T15:27:10.000Z","size":2652,"stargazers_count":140,"open_issues_count":8,"forks_count":13,"subscribers_count":9,"default_branch":"main","last_synced_at":"2024-12-28T07:07:13.584Z","etag":null,"topics":["chatbot","chatgpt","hacktoberfest","interpreter","jupyter","jupyter-lab","jupyter-notebooks","noteable","openai"],"latest_commit_sha":null,"homepage":"https://chatlab.dev","language":"Jupyter Notebook","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rgbkrk.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-04-06T22:40:56.000Z","updated_at":"2024-12-20T04:36:55.000Z","dependencies_parsed_at":"2024-01-14T09:58:12.874Z","dependency_job_id":"72256a3c-76f0-47b5-a33a-253f5fb14cdf","html_url":"https://github.com/rgbkrk/chatlab","commit_stats":null,"previous_names":["rgbkrk/murkrow","rgbkrk/chatlab"],"tags_count":64,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgbkrk%2Fchatlab","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgbkrk%2Fchatlab/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgbkrk%2Fchatlab/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rgbkrk%2Fchatlab/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rgbkrk","download_url":"https://codeload.github.com/rgbkrk/chatlab/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247423516,"owners_count":20936626,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["chatbot","chatgpt","hacktoberfest","interpreter","jupyter","jupyter-lab","jupyter-notebooks","noteable","openai"],"created_at":"2024-08-02T01:01:48.958Z","updated_at":"2025-04-06T02:12:51.681Z","avatar_url":"https://github.com/rgbkrk.png","language":"Jupyter Notebook","funding_links":[],"categories":["Browser-extensions","Jupyter Notebook"],"sub_categories":[],"readme":"# ChatLab\n\n**Chat Experiments, Simplified**\n\n💬🔬\n\nChatLab 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.\n\nBest yet, it's interactive in the notebook!\n\n## Notebooks to get started with\n\n* [Learning the Basics](./notebooks/basics.ipynb)\n* [Recommend and Visualize Color Palettes](./notebooks/color-picker.ipynb)\n* [Introduction to the Function Registry](./notebooks/function-registry.ipynb)\n* [Creating Knowledge Graphs with Pydantic](./notebooks/knowledge-graph.ipynb)\n* [Direct Parallel Function Calling](./notebooks/parallel-function-calling.ipynb)\n* [Let the Model do some Data Science](./notebooks/the-data-science-helper.ipynb)\n\n\n## Introduction\n\n```python\nimport chatlab\nimport random\n\ndef flip_a_coin():\n    '''Returns heads or tails'''\n    return random.choice(['heads', 'tails'])\n\nchat = chatlab.Chat()\nchat.register(flip_a_coin)\n\nawait chat(\"Please flip a coin for me\")\n```\n\n\u003cdetails style=\"background:#DDE6ED;color:#27374D;padding:.5rem 1rem;borderRadius:5px\"\u003e\n\u003csummary\u003e\u0026nbsp;𝑓\u0026nbsp; Ran `flip_a_coin`\n\u003c/summary\u003e\n\u003cbr /\u003e\n\nInput:\n\n```json\n{}\n```\n\nOutput:\n\n```json\n\"tails\"\n```\n\n\u003c/details\u003e\n\n```markdown\nIt landed on tails!\n```\n\nIn the notebook, text will stream into a Markdown output and function inputs and outputs are a nice collapsible display, like with ChatGPT Plugins.\n\nTODO: Include GIF/mp4 of this in action\n\n### Installation\n\n```bash\npip install chatlab\n```\n\n### Configuration\n\nYou'll need to set your `OPENAI_API_KEY` environment variable. You can find your API key on your [OpenAI account page](https://platform.openai.com/account/api-keys). I recommend setting it in an `.env` file when working locally.\n\nOn hosted notebook environments, set it in your Secrets to keep it safe from prying LLM eyes.\n\n## What can `Chat`s enable _you_ to do?\n\n💬\n\nWhere `Chat`s take it next level is with _Chat Functions_. You can\n\n-   declare a function\n-   register the function in your `Chat`\n-   watch as Chat Models call your functions!\n\nYou may recall this kind of behavior from ChatGPT Plugins. Now, you can take this even further with your own custom code.\n\nAs an example, let's give the large language models the ability to tell time.\n\n```python\nfrom datetime import datetime\nfrom pytz import timezone, all_timezones, utc\nfrom typing import Optional\nfrom pydantic import BaseModel\n\ndef what_time(tz: Optional[str] = None):\n    '''Current time, defaulting to UTC'''\n    if tz is None:\n        pass\n    elif tz in all_timezones:\n        tz = timezone(tz)\n    else:\n        return 'Invalid timezone'\n\n    return datetime.now(tz).strftime('%I:%M %p')\n\nclass WhatTime(BaseModel):\n    tz: Optional[str] = None\n```\n\nLet's break this down.\n\n`what_time` is the function we're going to provide access to. Its docstring forms the `description` for the model while the schema comes from the pydantic `BaseModel` called `WhatTime`.\n\n```python\nimport chatlab\n\nchat = chatlab.Chat()\n\n# Register our function\nchat.register(what_time, WhatTime)\n```\n\nAfter that, we can call `chat` with direct strings (which are turned into user messages) or using simple message makers from `chatlab` named `user` and `system`.\n\n```python\nawait chat(\"What time is it?\")\n```\n\n\u003cdetails style=\"background:#DDE6ED;color:#27374D;padding:.5rem 1rem;borderRadius:5px\"\u003e\n\u003csummary\u003e\u0026nbsp;𝑓\u0026nbsp; Ran `what_time`\n\u003c/summary\u003e\n\u003cbr /\u003e\n\nInput:\n\n```json\n{}\n```\n\nOutput:\n\n```json\n\"11:19 AM\"\n```\n\n\u003c/details\u003e\n\n```markdown\nThe current time is 11:19 AM.\n```\n\n## Interface\n\nThe `chatlab` package exports\n\n### `Chat`\n\nThe `Chat` class is the main way to chat using OpenAI's models. It keeps a history of your chat in `Chat.messages`.\n\n#### `Chat.submit`\n\n`submit` is how you send all the currently built up messages over to OpenAI. Markdown output will display responses from the `assistant`.\n\n```python\nawait chat.submit('What would a parent who says \"I have to play zone defense\" mean? ')\n# Markdown response inline\nchat.messages\n```\n\n```js\n[{'role': 'user',\n  'content': 'What does a parent of three kids mean by \"I have to play zone defense\"?'},\n {'role': 'assistant',\n  'content': 'When a parent of three kids says \"I have to play zone defense,\" it means that they...\n```\n\n#### `Chat.register`\n\nYou can register functions with `Chat.register` to make them available to the chat model. The function's docstring becomes the description of the function while the schema is derived from the `pydantic.BaseModel` passed in.\n\n```python\nfrom pydantic import BaseModel\n\nclass WhatTime(BaseModel):\n    tz: Optional[str] = None\n\ndef what_time(tz: Optional[str] = None):\n    '''Current time, defaulting to UTC'''\n    if tz is None:\n        pass\n    elif tz in all_timezones:\n        tz = timezone(tz)\n    else:\n        return 'Invalid timezone'\n\n    return datetime.now(tz).strftime('%I:%M %p')\n\nchat.register(what_time, WhatTime)\n```\n\n#### `Chat.messages`\n\nThe 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.\n\n```python\nchat.messages = chat.messages[-100:]\n```\n\n### Messaging\n\n#### `human`/`user`\n\nThese functions create a message from the user to the chat model.\n\n```python\nfrom chatlab import human\n\nhuman(\"How are you?\")\n```\n\n```json\n{ \"role\": \"user\", \"content\": \"How are you?\" }\n```\n\n#### `narrate`/`system`\n\n`system` messages, also called `narrate` in `chatlab`, 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.\n\n```python\nfrom chatlab import narrate\n\nnarrate(\"You are a large bird\")\n```\n\n```json\n{ \"role\": \"system\", \"content\": \"You are a large bird\" }\n```\n\n## Development\n\nThis project uses poetry for dependency management. To get started, clone the repo and run\n\n```bash\npoetry install -E dev -E test\n```\n\nWe use `ruff` and `mypy`.\n\n## Contributing\n\nPull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frgbkrk%2Fchatlab","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frgbkrk%2Fchatlab","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frgbkrk%2Fchatlab/lists"}