PC & IT SUPPORT MADE EASY FORUM
Would you like to react to this message? Create an account in a few clicks or log in to continue.

RAG With Recovery Project Builder CrewAI & Ollama

Go down

RAG With Recovery Project Builder CrewAI & Ollama Empty RAG With Recovery Project Builder CrewAI & Ollama

Post by jamied_uk 9th July 2024, 15:01

Code:

# (c)J~Net 2024
#
# ./start_quest.sh
#
# https://jnet.forumotion.com/t2016-rag-with-recovery-project-builder-crewai-ollama#3108
import sys
import os
import time
from crewai import Agent, Task
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
import logging

load_dotenv()

llm=ChatOpenAI(
    model="crewai-dolphin-llama3",
    base_url="http://localhost:11434/v1"
)

# Configure logging
logging.basicConfig(filename='script.log', level=logging.ERROR)

# Ensure the 'files' directory exists
os.makedirs('files', exist_ok=True)

def read_existing_steps():
    steps=[]
    for filename in sorted(os.listdir('files')):
        if filename.startswith('step') and filename.endswith('.txt'):
            with open(f'files/{filename}', 'r') as file:
                steps.append(file.read())
    latest_output=""
    if os.path.exists('files/latest_output.txt'):
        with open('files/latest_output.txt', 'r') as file:
            latest_output=file.read()
    return steps, latest_output

def save_step_result(step_number, result):
    step_filename=f"files/step{step_number}.txt"
    with open(step_filename, 'w') as step_file:
        step_file.write(result)

def execute_task_chain(content, agent, start_step=0):
    tasks=content.splitlines()
    for index, task_description in enumerate(tasks[start_step:], start=start_step + 1):
        if task_description.strip():
            try:
                task_config={
                    'description': task_description.strip(),
                    'expected_output': 'No specific output expected',
                    'agent': agent
                }
                task=Task(**task_config)
                result=task.execute()
                print(result)
               
                # Save result to latest_output.txt
                with open('files/latest_output.txt', 'a') as f:
                    f.write(result + '\n')

                # Save step result
                save_step_result(index, result)
            except Exception as e:
                logging.error(f"Error occurred during task execution: {e}", exc_info=True)
                print(f"Error occurred during task execution: {e}")

def generate_filename(base_name):
    count=1
    while True:
        filename=f"files/{base_name}-{count}"
        if not os.path.exists(filename):
            return filename
        count += 1

def create_file(filename, content):
    try:
        with open(filename, 'w') as file:
            file.write(content)
        return f"File '{filename}' created successfully."
    except Exception as e:
        logging.error(f"Error occurred while creating the file: {e}", exc_info=True)
        return f"An error occurred while creating the file: {e}"

def create_agent(role, goal, backstory):
    if os.path.exists('goal.txt'):
        with open('goal.txt', 'r') as goal_file:
            goal=goal_file.read().strip()

    return Agent(role=role, goal=goal, backstory=backstory, allow_delegation=False, verbose=True, llm=llm)

def main():
    existing_steps, latest_output=read_existing_steps()
    start_step=len(existing_steps)

    while True:
        prompt=input("Enter your command (type 'exit' to quit): ").strip()
        if prompt.lower() == 'exit':
            break

        general_agent=create_agent(
            role='Requirements Manager',
            goal="""Handle any user input commands including reading a file
                    and creating files for apps based on user input. For file reading,
                    read the contents and display them. For file creation,
                    generate a filename based on user input or a pattern and create
                    the file with the specified content. Respond as if
                    you were an expert developer providing file creation services
                    on demand.""",
            backstory="""You are an expert developer with a strong background
                        in software engineering. You provide high quality,
                        thorough, and efficient file creation services based on
                        user requirements."""
        )

        if prompt.lower().startswith('read '):
            start_time=time.time()
           
            filename=prompt[len('read '):].strip()
            content=read_file(filename)

            # Set the new goal description to the content read from the file
            general_agent.goal=content.strip()

            # Write the updated goal to goal.txt
            create_file('goal.txt', general_agent.goal)

            execute_task_chain(content, general_agent, start_step)
           
            end_time=time.time()
            elapsed_time=end_time - start_time
            elapsed_hours, rem=divmod(elapsed_time, 3600)
            elapsed_minutes, elapsed_seconds=divmod(rem, 60)
           
            print(f"Execution time: {int(elapsed_hours)}h {int(elapsed_minutes)}m {int(elapsed_seconds)}s")

        elif prompt.lower().startswith('create file '):
            parts=prompt[len('create file '):].split(' with content ', 1)
            if len(parts) == 2:
                filename, content=parts
                filename=generate_filename(filename.strip())
                result=create_file(filename, content.strip())
                print(result)
               
                # Save result to latest_output.txt
                with open('files/latest_output.txt', 'a') as f:
                    f.write(result + '\n')
            else:
                print("Invalid command format. Use: create file <filename> with content <content>")
        else:
            # Execute dynamic task based on user input
            task_description=prompt
            try:
                task=Task(description=task_description.strip(), agent=general_agent)
                result=task.execute()
                print(result)
               
                # Save result to latest_output.txt
                with open('files/latest_output.txt', 'a') as f:
                    f.write(result + '\n')
            except Exception as e:
                logging.error(f"Error occurred during task execution: {e}", exc_info=True)
                print(f"Error occurred during task execution: {e}")

def read_file(filename):
    try:
        with open(filename, 'r') as file:
            content=file.read()
        return content
    except FileNotFoundError:
        logging.error(f"File '{filename}' not found.")
        return f"File '{filename}' not found."

if __name__ == "__main__":
    if len(sys.argv) > 1:
        start_time=time.time()
       
        filename=sys.argv[1]
        content=read_file(filename)
        existing_steps, latest_output=read_existing_steps()
        start_step=len(existing_steps)
        general_agent=create_agent(
            role='Requirements Manager',
            goal="""Handle any user input commands including reading a file
                    and creating files for apps based on user input. For file reading,
                    read the contents and display them. For file creation,
                    generate a filename based on user input or a pattern and create
                    the file with the specified content. Respond as if
                    you were an expert developer providing file creation services
                    on demand.""",
            backstory="""You are an expert developer with a strong background
                        in software engineering. You provide high quality,
                        thorough, and efficient file creation services based on
                        user requirements."""
        )
        execute_task_chain(content, general_agent, start_step)
       
        end_time=time.time()
        elapsed_time=end_time - start_time
        elapsed_hours, rem=divmod(elapsed_time, 3600)
        elapsed_minutes, elapsed_seconds=divmod(rem, 60)
       
        print(f"Execution time: {int(elapsed_hours)}h {int(elapsed_minutes)}m {int(elapsed_seconds)}s")
    else:
        main()



Starting the quest

./start_quest.sh quest.txt




Code:
#!/bin/bash

# Print a blank line
echo ""

# Create a Python virtual environment in the "venv" directory
python -m venv venv

# Activate the virtual environment
source venv/bin/activate

# Upgrade pip to the latest version
#pip install --upgrade pip

# Install required packages from requirements.txt
#pip install -r requirements.txt

# Run the Python script
python questV3.py

# Deactivate the virtual environment
deactivate


Last edited by jamied_uk on 9th July 2024, 15:06; edited 1 time in total
jamied_uk
jamied_uk
Admin

Posts : 3000
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

RAG With Recovery Project Builder CrewAI & Ollama Empty Re: RAG With Recovery Project Builder CrewAI & Ollama

Post by jamied_uk 9th July 2024, 15:05

Cuda Support



Code:
import sys
import os
import time
from crewai import Agent, Task
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
import logging

load_dotenv()

# Hardcoded CUDA support configuration
use_cuda=os.getenv("USE_CUDA", "0")  # Default to "0" (CPU) if not specified

cuda_available=False
if use_cuda == "1":
    try:
        import torch  # Assuming PyTorch for CUDA support
        cuda_available=torch.cuda.is_available()
    except ImportError:
        cuda_available=False

llm=ChatOpenAI(
    model="crewai-dolphin-llama3",
    base_url="http://localhost:11434/v1",
    cuda=cuda_available
)

# Configure logging
logging.basicConfig(filename='script.log', level=logging.ERROR)

# Ensure the 'files' directory exists
os.makedirs('files', exist_ok=True)

def read_existing_steps():
    steps=[]
    for filename in sorted(os.listdir('files')):
        if filename.startswith('step') and filename.endswith('.txt'):
            with open(f'files/{filename}', 'r') as file:
                steps.append(file.read())
    latest_output=""
    if os.path.exists('files/latest_output.txt'):
        with open('files/latest_output.txt', 'r') as file:
            latest_output=file.read()
    return steps, latest_output

def save_step_result(step_number, result):
    step_filename=f"files/step{step_number}.txt"
    with open(step_filename, 'w') as step_file:
        step_file.write(result)

def execute_task_chain(content, agent, start_step=0):
    tasks=content.splitlines()
    for index, task_description in enumerate(tasks[start_step:], start=start_step + 1):
        if task_description.strip():
            try:
                task_config={
                    'description': task_description.strip(),
                    'expected_output': 'No specific output expected',  # Placeholder value for expected output
                    'agent': agent
                }
                task=Task(**task_config)
                result=task.execute()
                print(result)
                
                # Save result to latest_output.txt
                with open('files/latest_output.txt', 'a') as f:
                    f.write(result + '\n')

                # Save step result
                save_step_result(index, result)
            except Exception as e:
                logging.error(f"Error occurred during task execution: {e}", exc_info=True)
                print(f"Error occurred during task execution: {e}")

def generate_filename(base_name):
    count=1
    while True:
        filename=f"files/{base_name}-{count}"
        if not os.path.exists(filename):
            return filename
        count += 1

def create_file(filename, content):
    try:
        with open(filename, 'w') as file:
            file.write(content)
        return f"File '{filename}' created successfully."
    except Exception as e:
        logging.error(f"Error occurred while creating the file: {e}", exc_info=True)
        return f"An error occurred while creating the file: {e}"

def create_agent(role, goal, backstory):
    if os.path.exists('goal.txt'):
        with open('goal.txt', 'r') as goal_file:
            goal=goal_file.read().strip()

    return Agent(role=role, goal=goal, backstory=backstory, allow_delegation=False, verbose=True, llm=llm)

def main():
    existing_steps, latest_output=read_existing_steps()
    start_step=len(existing_steps)

    while True:
        prompt=input("Enter your command (type 'exit' to quit): ").strip()
        if prompt.lower() == 'exit':
            break

        general_agent=create_agent(
            role='Requirements Manager',
            goal="""Handle any user input commands including reading a file 
                    and creating files for apps based on user input. For file reading, 
                    read the contents and display them. For file creation,
                    generate a filename based on user input or a pattern and create 
                    the file with the specified content. Respond as if 
                    you were an expert developer providing file creation services 
                    on demand.""",
            backstory="""You are an expert developer with a strong background 
                        in software engineering. You provide high quality, 
                        thorough, and efficient file creation services based on 
                        user requirements."""
        )

        if prompt.lower().startswith('read '):
            start_time=time.time()
            
            filename=prompt[len('read '):].strip()
            content=read_file(filename)

            # Set the new goal description to the content read from the file
            general_agent.goal=content.strip()

            # Write the updated goal to goal.txt
            create_file('goal.txt', general_agent.goal)

            execute_task_chain(content, general_agent, start_step)
            
            end_time=time.time()
            elapsed_time=end_time - start_time
            elapsed_hours, rem=divmod(elapsed_time, 3600)
            elapsed_minutes, elapsed_seconds=divmod(rem, 60)
            
            print(f"Execution time: {int(elapsed_hours)}h {int(elapsed_minutes)}m {int(elapsed_seconds)}s")

        elif prompt.lower().startswith('create file '):
            parts=prompt[len('create file '):].split(' with content ', 1)
            if len(parts) == 2:
                filename, content=parts
                filename=generate_filename(filename.strip())
                result=create_file(filename, content.strip())
                print(result)
                
                # Save result to latest_output.txt
                with open('files/latest_output.txt', 'a') as f:
                    f.write(result + '\n')
            else:
                print("Invalid command format. Use: create file <filename> with content <content>")
        else:
            # Execute dynamic task based on user input
            task_description=prompt  # Use the entire user input as task description
            try:
                task_config={
                    'description': task_description.strip(),
                    'expected_output': 'No specific output expected',  # Placeholder value for expected output
                    'agent': general_agent
                }
                task=Task(**task_config)
                result=task.execute()
                print(result)
                
                # Save result to latest_output.txt
                with open('files/latest_output.txt', 'a') as f:
                    f.write(result + '\n')
            except Exception as e:
                logging.error(f"Error occurred during task execution: {e}", exc_info=True)
                print(f"Error occurred during task execution: {e}")

def read_file(filename):
    try:
        with open(filename, 'r') as file:
            content=file.read()
        return content
    except FileNotFoundError:
        logging.error(f"File '{filename}' not found.")
        return f"File '{filename}' not found."

if __name__ == "__main__":
    if len(sys.argv) > 1:
        start_time=time.time()
        
        filename=sys.argv[1]
        content=read_file(filename)
        existing_steps, latest_output=read_existing_steps()
        start_step=len(existing_steps)
        general_agent=create_agent(
            role='Requirements Manager',
            goal="""Handle any user input commands including reading a file 
                    and creating files for apps based on user input. For file reading, 
                    read the contents and display them. For file creation,
                    generate a filename based on user input or a pattern and create 
                    the file with the specified content. Respond as if 
                    you were an expert developer providing file creation services 
                    on demand.""",
            backstory="""You are an expert developer with a strong background 
                        in software engineering. You provide high quality, 
                        thorough, and efficient file creation services based on 
                        user requirements."""
        )
        execute_task_chain(content, general_agent, start_step)
        
        end_time=time.time()
        elapsed_time=end_time - start_time
        elapsed_hours, rem=divmod(elapsed_time, 3600)
        elapsed_minutes, elapsed_seconds=divmod(rem, 60)
        
        print(f"Execution time: {int(elapsed_hours)}h {int(elapsed_minutes)}m {int(elapsed_seconds)}s")
    else:
        main()
jamied_uk
jamied_uk
Admin

Posts : 3000
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

RAG With Recovery Project Builder CrewAI & Ollama Empty Re: RAG With Recovery Project Builder CrewAI & Ollama

Post by jamied_uk 9th July 2024, 15:31

.env file contains settings and vars!


Code:
OPENAI_API_BASE='http://localhost:11434/v1'
OPENAI_MODEL_NAME='crewai-dolphin-llama3'
OPENAI_API_KEY=NA
jamied_uk
jamied_uk
Admin

Posts : 3000
Join date : 2010-05-09
Age : 41
Location : UK

https://jnet.sytes.net

Back to top Go down

RAG With Recovery Project Builder CrewAI & Ollama Empty Re: RAG With Recovery Project Builder CrewAI & Ollama

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum