Package: cosmoai
pipinstallcosmoaicosmoCosmoAIlets LLMs run code (Python, Javascript, Shell, and more) locally. You can chat with CosmoAI through a ChatGPT-like interface in your terminal by running$ cosmoafter installing.This provides a natural-language interface to your computer's general-purpose capabilities:Create and edit photos, videos, PDFs, etc.Control a Chrome browser to perform researchPlot, clean, and analyze large datasets...etc.⚠️ Note: You'll be asked to approve code before it's run.Demohttps://github.com/SerHermesAn interactive demo is also available on Google Colab:[Along with an example implementation of a voice interface (inspired byHer):[Quick StartpipinstallcosmoaiTerminalAfter installation, simply runcosmo:cosmoPythonimportcosmocosmo.chat("Plot AAPL and META's normalized stock prices")# Executes a single commandcosmo.chat()# Starts an interactive chatComparison to ChatGPT's Code InterpreterOpenAI's release ofCode Interpreterwith GPT-4 presents a fantastic opportunity to accomplish real-world tasks with ChatGPT.However, OpenAI's service is hosted, closed-source, and heavily restricted:No internet access.Limited set of pre-installed packages.100 MB maximum upload, 120.0 second runtime limit.State is cleared (along with any generated files or links) when the environment dies.CosmoAI overcomes these limitations by running in your local environment. It has full access to the internet, isn't restricted by time or file size, and can utilize any package or library.This combines the power of GPT-4's Code Cosmo with the flexibility of your local development environment.CommandsUpdate:The Generator Update (0.1.5) introduced streaming:message="What operating system are we on?"forchunkincosmo.chat(message,display=False,stream=True):print(chunk)Interactive ChatTo start an interactive chat in your terminal, either runcosmofrom the command line:cosmoOrcosmo.chat()from a .py file:cosmo.chat()You can also stream each chunk:message="What operating system are we on?"forchunkincosmo.chat(message,display=False,stream=True):print(chunk)Programmatic ChatFor more precise control, you can pass messages directly to.chat(message):cosmo.chat("Add subtitles to all videos in /videos.")# ... Streams output to your terminal, completes task ...cosmo.chat("These look great but can you make the subtitles bigger?")# ...Start a New ChatIn Python, CosmoAI remembers conversation history. If you want to start fresh, you can reset it:cosmo.reset()Save and Restore Chatscosmo.chat()returns a List of messages, which can be used to resume a conversation withcosmo.messages = messages:messages=cosmo.chat("My name is Henry.")# Save messages to 'messages'cosmo.reset()# Reset cosmo ("Henry" will be forgotten)cosmo.messages=messages# Resume chat from 'messages' ("Henry" will be remembered)Customize System MessageYou can inspect and configure CosmoAI's system message to extend its functionality, modify permissions, or give it more context.cosmo.system_message+="""Run shell commands with -y so the user doesn't have to confirm them."""print(cosmo.system_message)Change your Language ModelCosmoAI usesLiteLLMto connect to hosted language models.You can change the model by setting the model parameter:cosmo--modelgpt-3.5-turbo
cosmo--modelclaude-2
cosmo--modelcommand-nightlyIn Python, set the model on the object:cosmo.model="gpt-3.5-turbo"Find the appropriate "model" string for your language model here.Running CosmoAI locallyCosmoAI usesLM Studioto connect to local language models (experimental).Simply runcosmoin local mode from the command line:cosmo--localYou will need to run LM Studio in the background.Downloadhttps://lmstudio.ai/then start it.Select a model then click↓ Download.Click the↔️button on the left (below 💬).Select your model at the top, then clickStart Server.Once the server is running, you can begin your conversation with CosmoAI.(When you run the commandcosmo --local, the steps above will be displayed.)Note:Local mode sets yourcontext_windowto 3000, and yourmax_tokensto 600. If your model has different requirements, set these parameters manually (see below).Context Window, Max TokensYou can modify themax_tokensandcontext_window(in tokens) of locally running models.For local mode, smaller context windows will use less RAM, so we recommend trying a much shorter window (~1000) if it's is failing / if it's slow. Make suremax_tokensis less thancontext_window.cosmo--local--max_tokens1000--context_window3000Debug modeTo help contributors inspect CosmoAI,--debugmode is highly verbose.You can activate debug mode by using it's flag (cosmo --debug), or mid-chat:$cosmo
...
>%debugtrue<-Turnsondebugmode

>%debugfalse<-TurnsoffdebugmodeInteractive Mode CommandsIn the interactive mode, you can use the below commands to enhance your experience. Here's a list of available commands:Available Commands:%debug [true/false]: Toggle debug mode. Without arguments or withtrueit
enters debug mode. Withfalseit exits debug mode.%reset: Resets the current session's conversation.%undo: Removes the previous user message and the AI's response from the message history.%save_message [path]: Saves messages to a specified JSON path. If no path is provided, it defaults tomessages.json.%load_message [path]: Loads messages from a specified JSON path. If no path is provided, it defaults tomessages.json.%tokens [prompt]: (Experimental) Calculate the tokens that will be sent with the next prompt as context and estimate their cost. Optionally calculate the tokens and estimated cost of apromptif one is provided. Relies onLiteLLM'scost_per_token()methodfor estimated costs.%help: Show the help message.ConfigurationCosmoAI allows you to set default behaviors using aconfig.yamlfile.This provides a flexible way to configure the cosmo without changing command-line arguments every time.Run the following command to open the configuration file:cosmo --configMultiple Configuration FilesCosmoAI supports multipleconfig.yamlfiles, allowing you to easily switch between configurations via the--config_fileargument.Note:--config_fileaccepts either a file name or a file path. File names will use the default configuration directory, while file paths will use the specified path.To create or edit a new configuration, run:cosmo --config --config_file $config_pathTo have CosmoAI load a specific configuration file run:cosmo --config_file $config_pathNote: Replace$config_pathwith the name of or path to your configuration file.CLI ExampleCreate a newconfig.turbo.yamlfilecosmo --config --config_file config.turbo.yamlEdit theconfig.turbo.yamlfile to setmodeltogpt-3.5-turboRun CosmoAI with theconfig.turbo.yamlconfigurationcosmo --config_file config.turbo.yamlPython ExampleYou can also load configuration files when calling CosmoAI from Python scripts:importosimportcosmocurrentPath=os.path.dirname(os.path.abspath(__file__))config_path=os.path.join(currentPath,'./config.test.yaml')cosmo.extend_config(config_path=config_path)message="What operating system are we on?"forchunkincosmo.chat(message,display=False,stream=True):print(chunk)Sample FastAPI ServerThe generator update enables CosmoAI to be controlled via HTTP REST endpoints:# server.pyfromfastapiimportFastAPIfromfastapi.responsesimportStreamingResponseimportcosmoapp=FastAPI()@app.get("/chat")defchat_endpoint(message:str):defevent_stream():forresultincosmo.chat(message,stream=True):yieldf"data:{result}\n\n"returnStreamingResponse(event_stream(),media_type="text/event-stream")@app.get("/history")defhistory_endpoint():returncosmo.messagespipinstallfastapiuvicorn
uvicornserver:app--reloadSafety NoticeSince generated code is executed in your local environment, it can interact with your files and system settings, potentially leading to unexpected outcomes like data loss or security risks.⚠️ CosmoAI will ask for user confirmation before executing code.You can runcosmo -yor setcosmo.auto_run = Trueto bypass this confirmation, in which case:Be cautious when requesting commands that modify files or system settings.Watch CosmoAI like a self-driving car, and be prepared to end the process by closing your terminal.Consider running CosmoAI in a restricted environment like Google Colab or Replit. These environments are more isolated, reducing the risks of executing arbitrary code.There isexperimentalsupport for asafe modeto help mitigate some risks.How Does it Work?CosmoAI equips afunction-calling language modelwith anexec()function, which accepts alanguage(like "Python" or "JavaScript") andcodeto run.We then stream the model's messages, code, and your system's outputs to the terminal as Markdown.ContributingThank you for your interest in contributing! We welcome involvement from the community.Please see ourContributing Guidelinesfor more details on how to get involved.LicenseCosmoAI and associated documentation files (the "Software"), are proprietary to [HENRY BLACK], and are confidential and protected under applicable copyright and other laws.Unauthorized use, reproduction or distribution of the Software or any portion of it, may result in severe civil and criminal penalties, and will be prosecuted to the maximum extent possible under the law.The Software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement.In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the Software or the use or other dealings in the Software.Note: This software is not affiliated with OpenAI.Having access to a junior programmer working at the speed of your fingertips ... can make new workflows effortless and efficient, as well as open the benefits of programming to new audiences.—OpenAI's Code Interpreter Release