{"id":18695123,"url":"https://github.com/rybla/chatscript","last_synced_at":"2025-11-08T13:30:33.583Z","repository":{"id":219999378,"uuid":"622824710","full_name":"rybla/chatscript","owner":"rybla","description":"This project provides a simple python interface to writing chatscripts for generating sequences of calls to OpenAI's GPT chat API. ","archived":false,"fork":false,"pushed_at":"2023-04-17T15:52:43.000Z","size":222,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2024-12-28T03:21:09.187Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rybla.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"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}},"created_at":"2023-04-03T06:24:08.000Z","updated_at":"2023-04-29T07:07:42.000Z","dependencies_parsed_at":null,"dependency_job_id":"06ba67a5-765d-4f19-8f2c-cda8872ebf4b","html_url":"https://github.com/rybla/chatscript","commit_stats":null,"previous_names":["riib11/chatscript","rybla/chatscript"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rybla%2Fchatscript","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rybla%2Fchatscript/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rybla%2Fchatscript/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rybla%2Fchatscript/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rybla","download_url":"https://codeload.github.com/rybla/chatscript/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239558907,"owners_count":19658927,"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":[],"created_at":"2024-11-07T11:14:02.964Z","updated_at":"2025-11-08T13:30:33.537Z","avatar_url":"https://github.com/rybla.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# chatscript (Python)\n\nThis project provides a simple python interface to writing __chatscripts__ for\ngenerating sequences of calls to OpenAI's GPT chat API. A __chatscript__ is an\nimperative script (this project provides an interface to Python, but the same\ninterface generalizes to any imperative programming language) that can create\n`Conversation` objects and make _message_ calls to it. \n\n```python\nderivativebot = Conversation(\"derivativebot\")\n```\n\nThere are three kinds of messages: `user`, `system`, and `assistant`. The\nfollowing script show how to use these messages together in a simple script.\n\n```python\nfrom os import path\n\n\"\"\"\n# derivativebot\n\nThis program defines a chatscript for asking for the derivatives and \nexplanations of how to take the derivatives of a collection of user-specified \nfunctions. The results are written to {answers_filepath}.\n\"\"\"\n\nanswers_filepath = \"derivatives.md\"\n\nfunctions = [\n  \"12\",\n  \"2 x^2\",\n  \"e^3\",\n  \"10 log(x)\"\n]\n\nif not path.exists(answers_filepath):\n  with open(answers_filepath, \"w+\") as file:\n    file.write(\"# Derivative Examples\\n\\n\")\n\n\n# A 'system' message corresponds to an initialization of the assistant that \n# tells it how to behave.\nderivativebot.system(\"\"\"\nYou are high school calculus tutor. Answer the user's questions will detailed step-by-step reasoning about how to take the derivative of a function. In your answer, first write the derivative of the user's function. Then on the next line after that, write a step-by-step explanation of how to calculate that derivative.\n\"\"\")\n\nfor function in functions:\n  # A 'user' message corresponds corresponds to a message from the user to the \n  # asssistant.\n  derivativebot.user(f\"What is the derivative {function}$?\")\n  # An 'assistant' message corresonds to a query to the GPT chat API.\n  answer = derivativebot.assistant()\n\n  # interprete the answer\n  lines = answer.splitlines()\n  derivative = lines[0].strip()\n  explanation = \"\\n\".join(lines[1:]).strip()\n\n  with open(answers_filepath, \"a+\") as file:\n    file.write(f\"\"\"\n{function}\n- derivative: {derivative}\n- explanation: {explanation}\n\n\"\"\")\n```\n\n## Installation\n\nTo install this project as a python module:\n\n```\nmake install\n```\n\n## Organization\n\n- `Conversation.py`: Defines the class `Conversation` which is the main\n  interface to chatscripts provided by this project.\n- `OpenAIClient.py`: Defines a simple wrapper class `OpenAIClient` for making\n  calls to OpenAI's chat API. Exposes static instance `OpenAIClient.instances`.\n- `conversations/`: some examples of histories and results from example\n  chatscripts I've written.\n\n## API Key\n\nTo make OpenAI API calls, you need to specify an OpenAI API\nconfiguration. By default, looks for `openai.json`. The configuration file has\nthe following format:\n```\n{\n    'api_key': '\u003c\u003cyour OpenAI API key\u003e\u003e',\n    'organization_id': '\u003c\u003coptionally, your OpenAI organization id\u003e\u003e'\n}\n```\n\n## Usage Details\n\nThe module `Conversation.py` defines a class `Conversation` that corresponds to\na single line of conversation between you (the user) and the assistant. A\n`Conversation` requires a name which lets it write to a file a _cache of results\nof the previous run_. Whenever the chatscript is run, the messages generated\nfor sending to the chat API (at each `Conversation.assistant(...)` call) are\nfirst compared to the cache of the previous state of the conversation. If none\nof the messages _before_ this assistant message have changed, and there exists\nan assistant response already in the cache, then the previous assistant response\nis used for this run of the chatscript instead of the calling the chat API\nagain.\n\nThis can be very useful when tinkering with a chatscript. For example when you\nrun the script again after appending more messages to it, it won't re-query at\nassistant calls before the new messages.\n\nThis behavior can be modified by some optional arguments to\n`Conversation.assistant`:\n\n```python\nConversation.assistant(\n  self,\n  # Explicitly specify content to overwrite the cached assistant response. If \n  # {content == None} then this yields an API call the response of which is \n  # returned by this function and written to the cache. If {content != None} \n  # then {content} is used as if it was the API response and no actual API call \n  # is made.\n  content: str = None, \n  # Use {content} to overwrite the cached assistant response; if \n  # {content == None} then this result in an API call.\n  overwrite = False \n)\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frybla%2Fchatscript","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frybla%2Fchatscript","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frybla%2Fchatscript/lists"}