LettreAI Documentation
  • Home
  • User Guide
  • Nutshell
  • Manual
  • Examples
  • API
  1. Manual
  2. LLM Integration - Claude
  • Getting Started
    • AI Task Documentation
    • Installation
    • Quick Start
  • User Guide
    • User Guide
  • Nutshell
    • AI-Task in a Nutshell
  • Manual
    • Core Concepts
    • Configuration
    • Instructions
    • Productions
    • Functions
    • LLM Integration - Claude
  • Examples
    • Basic Examples
    • Advanced Examples
    • Instruction Examples
    • Production Examples
  • Reports
    • AI Task Reports
  • API Reference
    • Pipeline API
    • Engine API
    • Functions API
  • Development
    • Contributing
    • Architecture
  1. Manual
  2. LLM Integration - Claude

LLM Integration - Claude

Using Claude in AI-Task

This guide explains how to use Anthropic’s Claude models in your AI-Task pipelines, with a focus on code execution.

Basic Claude Configuration

To use Claude, first set up your API key:

export ANTHROPIC_API_KEY=your_key_here

Running Code with Claude

Claude can execute code as part of your task pipeline. Here’s how to set it up:

1. Basic Code Execution

Create a task file code_exec.ai:

name: "Code Execution"
description: "Execute Python code with Claude"

pipe:
  - type: "llm"
    tmpl: |
      SYSTEM: You are a helpful AI assistant that can write and execute Python code.

      USER: Write a Python function that calculates the Fibonacci sequence up to n terms and run it for n=5.

      A: I'll write and execute the code:

      ```python
      def fibonacci(n):
          sequence = [0, 1]
          while len(sequence) < n:
              sequence.append(sequence[-1] + sequence[-2])
          return sequence

      # Test the function
      n = 5
      result = fibonacci(n)
      print(f"Fibonacci sequence up to {n} terms: {result}")
      ```
    model: "claude-3-7-sonnet-latest"

2. Interactive Code Development

For interactive code development, use the code pipe type:

name: "Interactive Code Development"
description: "Develop and test code interactively"

pipe:
  - type: "llm"
    tmpl: |
      SYSTEM: You are a helpful AI assistant that can write and execute Python code.

      USER: Create a simple data analysis script that:
      1. Reads a CSV file
      2. Calculates basic statistics
      3. Creates a plot

      A: I'll help you create a data analysis script:

      ```python
      import pandas as pd
      import matplotlib.pyplot as plt

      # Read the data
      df = pd.read_csv('data.csv')

      # Calculate statistics
      stats = df.describe()
      print("Basic statistics:")
      print(stats)

      # Create a plot
      plt.figure(figsize=(10, 6))
      df.plot(kind='bar')
      plt.title('Data Visualization')
      plt.savefig('plot.png')
      ```
    model: "claude-3-7-sonnet-latest"

  - type: "function"
    function: "airesult"
    params:
      file: "analysis_script.py"

3. Code with Dependencies

When your code requires external packages, specify them in the task:

name: "Code with Dependencies"
description: "Execute code with external dependencies"

requirements:
  - pandas
  - matplotlib
  - scikit-learn

pipe:
  - type: "llm"
    tmpl: |
      SYSTEM: You are a helpful AI assistant that can write and execute Python code.

      USER: Create a machine learning script that:
      1. Loads the iris dataset
      2. Trains a classifier
      3. Makes predictions

      A: I'll create a machine learning script using scikit-learn:

      ```python
      from sklearn.datasets import load_iris
      from sklearn.model_selection import train_test_split
      from sklearn.ensemble import RandomForestClassifier
      import pandas as pd

      # Load data
      iris = load_iris()
      X_train, X_test, y_train, y_test = train_test_split(
          iris.data, iris.target, test_size=0.2
      )

      # Train model
      clf = RandomForestClassifier()
      clf.fit(X_train, y_train)

      # Make predictions
      predictions = clf.predict(X_test)
      accuracy = clf.score(X_test, y_test)
      print(f"Model accuracy: {accuracy:.2f}")
      ```
    model: "claude-3-7-sonnet-latest"

Best Practices

  1. Error Handling: Always include error handling in your code:

    try:
        # Your code here
    except Exception as e:
        print(f"Error: {e}")
  2. Code Documentation: Request Claude to include comments and docstrings:

    tmpl: |
      SYSTEM: You are a helpful AI assistant that writes well-documented Python code.
    
      USER: Write a documented version of the code...
  3. Testing: Include test cases in your code:

    def test_function():
        assert function(input) == expected_output
  4. Resource Management: Use context managers for file operations:

    with open('file.txt', 'w') as f:
        f.write(content)

Common Issues and Solutions

  1. Memory Issues
    • Break large datasets into chunks
    • Use generators for large data processing
    • Clean up resources after use
  2. Package Conflicts
    • Use virtual environments
    • Specify exact package versions
    • Test dependencies before running
  3. Execution Timeouts
    • Break long operations into smaller steps
    • Add progress indicators
    • Implement checkpointing

Example: Complete Data Analysis Pipeline

Here’s a complete example that combines code execution with data analysis:

name: "Data Analysis Pipeline"
description: "End-to-end data analysis with code execution"

pipe:
  - type: "function"
    function: "aisource"
    params:
      file: "data/*.csv"

  - type: "llm"
    tmpl: |
      SYSTEM: You are a data analysis expert that can write and execute Python code.

      USER: Analyze the following data and create visualizations:
      {{ pipein_text }}

      A: I'll create a comprehensive analysis:

      ```python
      import pandas as pd
      import matplotlib.pyplot as plt
      import seaborn as sns
      from scipy import stats

      # Read and process data
      def analyze_data(data):
          # Basic statistics
          print("Basic Statistics:")
          print(data.describe())

          # Create visualizations
          plt.figure(figsize=(12, 6))
          sns.boxplot(data=data)
          plt.title('Data Distribution')
          plt.savefig('distribution.png')

          # Correlation analysis
          plt.figure(figsize=(10, 8))
          sns.heatmap(data.corr(), annot=True)
          plt.title('Correlation Matrix')
          plt.savefig('correlation.png')

          return {
              'stats': data.describe().to_dict(),
              'correlation': data.corr().to_dict()
          }

      # Execute analysis
      df = pd.read_csv('{{ pipein_text }}')
      results = analyze_data(df)
      ```
    model: "claude-3-7-sonnet-latest"

  - type: "function"
    function: "airesult"
    params:
      file: "analysis_results.json"

Next Steps

  • Learn about Template Design for code generation
  • Explore Function Integration with code execution
  • Check the Examples for more complex code execution patterns
Functions
Basic Examples

LettreAI Documentation

 
  • Edit this page
  • Report an issue
  • License: MIT