open-Cosmo 0.1.5
Let language models run code locally.
Requires Python: >=3.10,<4.0

<h1 align="center">● Open Cosmo</h1>

<p align="center">
    <a href="https://discord.gg/6p3fD6rBVm">
        <img alt="Discord" src="https://img.shields.io/discord/1146610656779440188?logo=discord&style=flat&logoColor=white">
    </a>
    <a href="README_JA.md"><img src="https://img.shields.io/badge/ドキュメント-日本語-white.svg" alt="JA doc"></a>
    <a href="README_ZH.md"><img src="https://img.shields.io/badge/文档-中文版-white.svg" alt="ZH doc"></a>
    <img src="https://img.shields.io/static/v1?label=license&message=MIT&color=white&style=flat" alt="License">
    <br><br>
    <b>Let language models run code on your computer.</b><br>
    An open-source, locally running implementation of OpenAI's Code Cosmo.<br>
    <br><a href="https://openCosmo.com">Get early access to the desktop application.</a><br>
</p>

<br>

![poster](https://github.com/KillianLucas/open-Cosmo/assets/63927363/08f0d493-956b-4d49-982e-67d4b20c4b56)

<br>

```shell
pip install open-Cosmo
```

```shell
Cosmo
```

<br>

**Open Cosmo** lets LLMs run code (Python, Javascript, Shell, and more) locally. You can chat with Open Cosmo through a ChatGPT-like interface in your terminal by running `$ Cosmo` after 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 research
-   Plot, clean, and analyze large datasets
-   ...etc.

**⚠️ Note: You'll be asked to approve code before it's run.**

<br>

## Demo

https://github.com/KillianLucas/open-Cosmo/assets/63927363/37152071-680d-4423-9af3-64836a6f7b60

#### An interactive demo is also available on Google Colab:

[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1WKmRXZgsErej2xUriKzxrEAXdxMSgWbb?usp=sharing)

## Quick Start

```shell
pip install open-Cosmo
```

### Terminal

After installation, simply run `Cosmo`:

```shell
Cosmo
```

### Python

```python
import Cosmo

Cosmo.chat("Plot AAPL and META's normalized stock prices") # Executes a single command
Cosmo.chat() # Starts an interactive chat
```

## Comparison to ChatGPT's Code Cosmo

OpenAI's release of [Code Cosmo](https://openai.com/blog/chatgpt-plugins#code-Cosmo) with 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](https://wfhbrian.com/mastering-chatgpts-code-Cosmo-list-of-python-packages/).
-   100 MB maximum upload, 120.0 second runtime limit.
-   State is cleared (along with any generated files or links) when the environment dies.

---

Open Cosmo overcomes these limitations by running on 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.

## Commands

### Interactive Chat

To start an interactive chat in your terminal, either run `Cosmo` from the command line:

```shell
Cosmo
```

Or `Cosmo.chat()` from a .py file:

```python
Cosmo.chat()
```

### Programmatic Chat

For more precise control, you can pass messages directly to `.chat(message)`:

```python
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 Chat

In Python, Open Cosmo remembers conversation history. If you want to start fresh, you can reset it:

```python
Cosmo.reset()
```

### Save and Restore Chats

`Cosmo.chat()` returns a List of messages when return_messages=True, which can be used to resume a conversation with `Cosmo.load(messages)`:

```python
messages = Cosmo.chat("My name is Killian.", return_messages=True) # Save messages to 'messages'
Cosmo.reset() # Reset Cosmo ("Killian" will be forgotten)

Cosmo.load(messages) # Resume chat from 'messages' ("Killian" will be remembered)
```

### Customize System Message

You can inspect and configure Open Cosmo's system message to extend its functionality, modify permissions, or give it more context.

```python
Cosmo.system_message += """
Run shell commands with -y so the user doesn't have to confirm them.
"""
print(Cosmo.system_message)
```

### Change the Model

For `gpt-3.5-turbo`, use fast mode:

```shell
Cosmo --fast
```

In Python, you will need to set the model manually:

```python
Cosmo.model = "gpt-3.5-turbo"
```

### Running Open Cosmo locally

ⓘ **Issues running locally?** Read our new [GPU setup guide](/docs/GPU.md) and [Windows setup guide](/docs/WINDOWS.md).

You can run `Cosmo` in local mode from the command line to use `Code Llama`:

```shell
Cosmo --local
```

Or run any Hugging Face model **locally** by using its repo ID (e.g. "tiiuae/falcon-180B"):

```shell
Cosmo --model tiiuae/falcon-180B
```

#### Local model params

You can easily modify the `max_tokens` and `context_window` (in tokens) of locally running models.

Smaller context windows will use less RAM, so we recommend trying a shorter window if GPU is failing.

```shell
Cosmo --max_tokens 2000 --context_window 16000
```

### Azure Support

To connect to an Azure deployment, the `--use-azure` flag will walk you through setting this up:

```shell
Cosmo --use-azure
```

In Python, set the following variables:

```
Cosmo.use_azure = True
Cosmo.api_key = "your_openai_api_key"
Cosmo.azure_api_base = "your_azure_api_base"
Cosmo.azure_api_version = "your_azure_api_version"
Cosmo.azure_deployment_name = "your_azure_deployment_name"
Cosmo.azure_api_type = "azure"
```

### Debug mode

To help contributors inspect Open Cosmo, `--debug` mode is highly verbose.

You can activate debug mode by using it's flag (`Cosmo --debug`), or mid-chat:

```shell
$ Cosmo
...
> %debug # <- Turns on debug mode
```

### Configuration with .env

Open Cosmo allows you to set default behaviors using a .env file. This provides a flexible way to configure the Cosmo without changing command-line arguments every time.

Here's a sample .env configuration:

```
Cosmo_CLI_AUTO_RUN=False
Cosmo_CLI_FAST_MODE=False
Cosmo_CLI_LOCAL_RUN=False
Cosmo_CLI_DEBUG=False
Cosmo_CLI_USE_AZURE=False
```

You can modify these values in the .env file to change the default behavior of the Open Cosmo.

## Safety Notice

Since 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.

**⚠️ Open Cosmo will ask for user confirmation before executing code.**

You can run `Cosmo -y` or set `Cosmo.auto_run = True` to bypass this confirmation, in which case:

-   Be cautious when requesting commands that modify files or system settings.
-   Watch Open Cosmo like a self-driving car, and be prepared to end the process by closing your terminal.
-   Consider running Open Cosmo in a restricted environment like Google Colab or Replit. These environments are more isolated, reducing the risks associated with executing arbitrary code.

## How Does it Work?

Open Cosmo equips a [function-calling language model](https://platform.openai.com/docs/guides/gpt/function-calling) with an `exec()` function, which accepts a `language` (like "Python" or "JavaScript") and `code` to run.

We then stream the model's messages, code, and your system's outputs to the terminal as Markdown.

# Contributing

Thank you for your interest in contributing! We welcome involvement from the community.

Please see our [Contributing Guidelines](/docs/contributing.md) for more details on how to get involved.

## License

Open Cosmo is licensed under the MIT License. You are permitted to use, copy, modify, distribute, sublicense and sell copies of 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 Cosmo Release_

<br>