Skip to main content

🚀 CodeAgent

Its Core Part To Create Agentic Generative AI

Automate code generation, execution, and debugging for your projects using LLM-powered agents.
Supports multiple providers (Proplexity, Gemini, and more), multimodal input, and dependency management.


📦 Installation

Install from PyPI:

pip install c4agent

Or install from source:

git clone https://github.com/yourusername/CodeAgent.git
cd CodeAgent
pip install -r requirements.txt

⚡ Quick Start

Initialize Agent

from Agent.CodeAgent import CodeAgent

agent = CodeAgent(
    provider="local",
    local_fn=generate,
    attempt_limit=10
)

✨ Usage

🔹 1. Generate Code from Prompt

agent.generate(
    "Explain About Artificial Intelligence"
).json()

🔹 2. Automate Flow - Example Project

prompt = """
You are an AI Agent. You will code like an AI research scientist.

Code For SmolAgents

Instructions:
1. Agent should answer tech-related questions
2. Execution not supported
3. Give only Python code
4. Python only support
5. Should include docstrings

User: Build a multimodal embedding model (Image + Text) using contrastive learning.

Dataset Link and Description:
- Kaggle credentials are already set up
- Dataset: fashion-product-images-small

Load dataset:
```python
!mkdir -p /root/.kaggle
!cp kaggle.json /root/.kaggle
!chmod 600 /root/.kaggle/kaggle.json
!kaggle datasets download paramaggarwal/fashion-product-images-small

Dataset load using Python:

import pandas as pd
df = pd.read_csv("/content/myntradataset/styles.csv", on_bad_lines="skip")
df.head()

Example dataset output:

   id    gender    masterCategory    subCategory    articleType    baseColour    season    year    usage    productDisplayName
0  15970  Men      Apparel         Topwear        Shirts        Navy Blue    Fall      2011.0  Casual   Turtle Check Men Navy Blue Shirt
1  39386  Men      Apparel         Bottomwear     Jeans         Blue         Summer   2012.0  Casual   Peter England Men Party Blue Jeans
2  59263  Women    Accessories     Watches        Watches       Silver       Winter   2016.0  Casual   Titan Women Silver Watch
3  21379  Men      Apparel         Bottomwear     Track Pants   Black        Fall      2011.0  Casual   Manchester United Men Solid Black Track Pants
4  53759  Men      Apparel         Topwear        Tshirts       Grey         Summer   2012.0  Casual   Puma Men Grey T-shirt

Model Requirements:

  • Use HuggingFace pretrained BERT and ViT models
  • Train using contrastive learning
  • Use Torch and optionally LangChain
  • Save best model & logs
  • Include evaluation, testing, and CUDA support
  • Progress bar using tqdm
  • Provide full final code """

Run the agent

agent(prompt)


### 🔹 3. V3 Multimodal Example

```python
agent = CodeAgent(
    gemini_apikey="<apikey>",
    provider="gemini"
)

result = agent({
    "text": "Write a Python script to save a plot in ./plot.png",
    "images": ["/content/Loss.png", "/content/Accuracy.png"]
})

print(result)

📂 Outputs are stored in local folders.

📑 Example Output

When running prompts, CodeAgent will:

  • ✅ Generate full Python code
  • ✅ Manage dependencies
  • ✅ Save outputs & logs locally
  • ✅ Handle debugging & execution automatically

🔧 Requirements

  • Python 3.8+
  • Dependencies (auto-installed with pip install c4agent)

📌 Roadmap

  • Support Proplexity provider
  • Add Gemini provider
  • Dependency manager
  • Multimodal input (text + images)
  • Add more providers (OpenAI, Claude, etc.)
  • CLI support
  • Web UI for interactive coding

🤝 Contributing

Contributions are welcome!

  1. Fork the repo
  2. Create your feature branch (git checkout -b feature/awesome-feature)
  3. Commit changes (git commit -m 'Add awesome feature')
  4. Push to branch (git push origin feature/awesome-feature)
  5. Open a Pull Request

📜 License

MIT License © 2025

🌟 Support

If you like this project, please ⭐ the repo to support development!


V3 API Documentation

class CodeAgent:
    """
    A multimodal code generation and execution agent.

    CodeAgent interacts with multiple LLM providers (Perplexity, Gemini, Anthropic, OpenAI),
    handles multimodal inputs (text, images, PDFs), generates Python code, installs missing
    dependencies, and executes code with iterative debugging.

    :param pplx_apikey: Perplexity API key.
    :type pplx_apikey: str, optional
    :param gemini_apikey: Gemini API key.
    :type gemini_apikey: str, optional
    :param anthropic_apikey: Anthropic API key.
    :type anthropic_apikey: str, optional
    :param openai_apikey: OpenAI API key.
    :type openai_apikey: str, optional
    :param provider: Model provider, one of {"perplexity", "gemini", "anthropic", "openai"}.
    :type provider: str, default="perplexity"
    :param model: Model identifier; if None, defaults to provider’s default.
    :type model: str, optional
    """

    def generate(self, input_data: Union[str, dict]) -> str:
        """
        Generate a response from the configured provider.

        :param input_data: Either a plain prompt (str) or a dict containing
            ``{"text": str, "images": [paths], "pdfs": [paths]}``.
        :type input_data: str or dict
        :return: Model output as string.
        :rtype: str
        :raises ValueError: If provider is unknown.
        :raises requests.HTTPError: If API request fails.
        """
        ...

    def process_multimodal_input(self, input_data: Union[str, dict]) -> dict:
        """
        Process multimodal inputs into normalized format.

        :param input_data: Input string or dict with keys ``text``, ``images``, ``pdfs``.
        :type input_data: str or dict
        :return: Dictionary with keys ``text``, ``images``, ``files``.
        :rtype: dict
        :raises ValueError: If input is neither string nor dict.
        """
        ...

    def is_stdlib_package(self, package: str) -> bool:
        """
        Check if a package is part of the Python standard library.

        :param package: Package name.
        :type package: str
        :return: True if stdlib, False otherwise.
        :rtype: bool
        """
        ...

    def parse_imports(self, code: str) -> List[str]:
        """
        Extract imports from code using AST.

        :param code: Python source code.
        :type code: str
        :return: List of imported top-level modules.
        :rtype: list[str]
        """
        ...

    def extract_requirements_from_code(self, code: str) -> List[str]:
        """
        Extract requirements from a ``# Requirements:`` comment.

        :param code: Python source code.
        :type code: str
        :return: List of requirement strings.
        :rtype: list[str]
        """
        ...

    def generate_requirements(self, packages: list, filename: str = "./outputs/requirements.txt"):
        """
        Generate or update requirements.txt with detected dependencies.

        :param packages: List of package names.
        :type packages: list[str]
        :param filename: Path to requirements file.
        :type filename: str
        """
        ...

    def install_missing_packages(self, packages: List[str]) -> tuple[int, str, str]:
        """
        Install missing packages via pip.

        :param packages: List of package names with optional version specifiers.
        :type packages: list[str]
        :return: (return_code, stdout, stderr)
        :rtype: tuple[int, str, str]
        """
        ...

    def dependency_manager(self, code: str) -> tuple[int, str, str]:
        """
        Detect and install dependencies based on code.

        - Parses imports and requirements.
        - Adds provider-specific deps (anthropic, openai).
        - Adds Pillow/PyPDF2 if handling images/PDFs.

        :param code: Python source code.
        :type code: str
        :return: (return_code, stdout, stderr)
        :rtype: tuple[int, str, str]
        """
        ...

    def response_to_pycode(self, response: str) -> Optional[str]:
        """
        Extract Python code from model response (inside ```python ...```).

        :param response: Model response text.
        :type response: str
        :return: Extracted Python code or None.
        :rtype: str or None
        """
        ...

    def response_to_pyfile(self, response: str, filename: str = "./outputs/pycode.py"):
        """
        Save extracted Python code to file.

        :param response: Model response text.
        :type response: str
        :param filename: Path to output file.
        :type filename: str
        :raises ValueError: If no Python code block found.
        """
        ...

    def run_script_realtime(self, filepath: str = "./outputs/pycode.py") -> tuple[int, str, str]:
        """
        Run Python script with real-time stdout/stderr capture.

        :param filepath: Path to script.
        :type filepath: str
        :return: (return_code, stdout, stderr)
        :rtype: tuple[int, str, str]
        :raises FileNotFoundError: If file not found.
        """
        ...

    def __call__(self, input_data: Union[str, dict]) -> tuple[int, str, str]:
        """
        Generate, install dependencies, run, and debug Python code.

        - Ensures output is a valid Python code block with ``# Requirements:``.
        - Saves to file and installs dependencies.
        - Runs script and retries debugging up to 10 times if it fails.
        - Updates requirements.txt on success.

        :param input_data: Prompt string or dict with multimodal input.
        :type input_data: str or dict
        :return: (exit_code, stdout, stderr)
        :rtype: tuple[int, str, str]
        """
        ...

Metadata

Release files for c4agent 1.21

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for c4agent 1.21
File Size Uploaded
c4agent-1.21.tar.gz 20.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for c4agent 1.21
File Interpreter ABI Platform
c4agent-1.21-py3-none-any.whl Python 3 none any Details

Total release size: 45.4 kB

Release files / c4agent-1.21.tar.gz

Download URL c4agent-1.21.tar.gz
Size 20.9 kB
Tags Source
SHA-256 checksum
How to use checksums
90c83d63b725657173da581c0ee741453c1544855b398e6c5c3e50df6c5cc348
BLAKE2b-256 checksum
How to use checksums
1bb88ee5d5151baa07166d35c231dcb6e489b35f95dbbecb1fa66666a33a33ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release files / c4agent-1.21-py3-none-any.whl

Download URL c4agent-1.21-py3-none-any.whl
Size 24.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bf0becf607bfef66e5f9daefa500a94a5649abd3363a137a4b07d39726cca8d1
BLAKE2b-256 checksum
How to use checksums
19db244bd104bcd47ab7e510d937abcf590eda15c8da6e22dd717ba37e10e83e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.13

Release history Release notifications | RSS feed

This release

1.21 This release

2 release files

1.2

2 release files

1.1

2 release files

0.1.2

2 release files

0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page