Categories
Praison AI

Praison AI Agents.yml

framework: crewai
topic: Artificial Intelligence
roles:
  movie_concept_creator:
    backstory: 'Creative thinker with a deep understanding of cinematic storytelling,
      capable of using AI-generated storylines to create unique and compelling movie
      ideas.'
    goal: Generate engaging movie concepts using AI storylines
    role: Movie Concept Creator
    tasks:
      movie_concept_development:
        description: 'Develop movie concepts from AI-generated storylines, ensuring
          they are engaging and have strong narrative arcs.'
        expected_output: 'Well-structured movie concept document with character
          bios, settings, and plot outlines.'
  screenwriter:
    backstory: 'Expert in writing engaging dialogue and script structure, able to
      turn movie concepts into production-ready scripts.'
    goal: Write compelling scripts based on movie concepts
    role: Screenwriter
    tasks:
      scriptwriting_task:
        description: 'Turn movie concepts into polished scripts with well-developed
          characters, strong dialogue, and effective scene transitions.'
        expected_output: 'Production-ready script with a beginning, middle, and
          end, along with character development and engaging dialogues.'
  editor:
    backstory: 'Adept at identifying inconsistencies, improving language usage,
      and maintaining the overall flow of the script.'
    goal: Refine the scripts and ensure continuity of the movie storyline
    role: Editor
    tasks:
      editing_task:
        description: 'Review, edit, and refine the scripts to ensure they are cohesive
          and follow a well-structured narrative.'
        expected_output: 'A polished final draft of the script with no inconsistencies,
          strong character development, and effective dialogue.'
dependencies: []
Categories
OpenAI

OpenAI API Key, Base URL and Model Hard Coded

from openai import OpenAI
client = OpenAI(api_key="sk-xxxxx", base_url="https://api.openai.com/v1")

response = client.chat.completions.create(
  model="gpt-3.5-turbo-0125",
  messages=[
    {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
    {"role": "user", "content": "Who won the world series in 2020?"}
  ]
)
print(response.choices[0].message.content)
Categories
Ollama

Ollama Json Response Code

from openai import OpenAI
from pydantic import BaseModel, Field
# client = OpenAI(api_key="xxx", base_url="http://localhost:11434/v1")

import instructor

class WinnerData(BaseModel):
    winner: str = Field(..., description="Winner")

class WinnerInfo(BaseModel):
    data: WinnerData

# enables `response_model` in create call
client = instructor.patch(
    OpenAI(
        base_url="http://localhost:11434/v1",
        api_key="ollama",
    ),
    mode=instructor.Mode.JSON,
)

response = client.chat.completions.create(
  model="mistral",
  response_model=WinnerInfo,
  messages=[
    {"role": "system", "content": "You are a helpful assistant designed to output JSON."},
    {"role": "user", "content": "Who won the world series in 2020?"}
  ]
)
print(response.data.winner)
print(response.model_dump_json(indent=2))
Categories
Python

Identify Error causing file

import tracemalloc
import logging
import warnings
import sys
import traceback

# Enable Tracemalloc
tracemalloc.start()

# Configure Logging
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(filename)s - %(funcName)s - %(lineno)d - %(message)s')

# Function to Log Warnings with Traceback
def warn_with_traceback(message, category, filename, lineno, file=None, line=None):
    log = file if hasattr(file, 'write') else sys.stderr
    traceback.print_stack(file=log)
    log.write(warnings.formatwarning(message, category, filename, lineno, line))

# Override showwarning
warnings.showwarning = warn_with_traceback
Categories
RAG

AI Research RAG Using ChromaDB Ollama

import gradio as gr
import os
import time
import arxiv
from langchain_community.vectorstores import Qdrant
from langchain_community.vectorstores import Chroma
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
from langchain_community.chat_models import ChatOllama
from langchain.prompts import ChatPromptTemplate
from langchain.pydantic_v1 import BaseModel
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnableParallel, RunnablePassthrough
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import GPT4AllEmbeddings
from langchain_community.embeddings.ollama import OllamaEmbeddings

def init_or_load_chroma(path="./tmp/local_chroma", collection_name="llm_dataset_survey_papers"):
    try:
        chroma_instance = Chroma(
            persist_directory=path,
        )
        print("Existing embeddings loaded.")
    except Exception as e:
        print("No existing embeddings found, starting new Chroma instance.", e)
        chroma_instance = None

    return chroma_instance, chroma_instance if chroma_instance else None

# Attempt to load existing Chroma instance
chroma, retriever = init_or_load_chroma()

def process_papers(query):
    dirpath = "llm_dataset_survey_papers"
    if not os.path.exists(dirpath):
        os.makedirs(dirpath)
    
    client = arxiv.Client()
    search = arxiv.Search(
        query=query,
        max_results=10,
        sort_order=arxiv.SortOrder.Descending
    )
    
    for result in client.results(search):
        while True:
            try:
                result.download_pdf(dirpath=dirpath)
                print(result)
                print(f"-> Paper id {result.get_short_id()} with title '{result.title}' is downloaded.")
                break
            except (FileNotFoundError, ConnectionResetError) as e:
                print("Error occurred:", e)
                time.sleep(5)
    
    papers = []
    loader = DirectoryLoader(dirpath, glob="./*.pdf", loader_cls=PyPDFLoader)
    try:
        papers = loader.load()
    except Exception as e:
        print(f"Error loading file: {e}")
    full_text = ''
    for paper in papers:
        full_text += paper.page_content
    
    full_text = " ".join(line for line in full_text.splitlines() if line)
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
    paper_chunks = text_splitter.create_documents([full_text])
    
    global qdrant, retriever
    qdrant = Qdrant.from_documents(
        documents=paper_chunks,
        embedding=OllamaEmbeddings(model='nomic-embed-text'),
        path="./tmp/local_qdrant",
        collection_name="llm_dataset_survey_papers",
    )
    retriever = qdrant.as_retriever()
    
    return "Papers processed and saved to embeddings database."

def perform_query(question_text):
    global retriever
    if not retriever:
        return "Error: No papers processed. Please process papers first."
    
    template = """Answer the question based only on the following context:
{context}

Question: {question}
"""
    prompt = ChatPromptTemplate.from_template(template)
    
    ollama_llm = "llama2:7b-chat"
    model = ChatOllama(model=ollama_llm)
    
    chain = (
        RunnableParallel({"context": retriever, "question": RunnablePassthrough()})
        | prompt
        | model
        | StrOutputParser()
    )
    
    class Question(BaseModel):
        __root__: str
    
    chain = chain.with_types(input_type=Question)
    result = chain.invoke(Question(__root__=question_text))
    return result

with gr.Blocks() as demo:
    with gr.Tab("Process Papers"):
        query_input = gr.Text(label="Search Query")
        process_button = gr.Button("Process Papers")
        process_output = gr.Text(label="Process Output")
        process_button.click(process_papers, inputs=query_input, outputs=process_output)

    with gr.Tab("Query Retriever"):
        question_input = gr.Text(label="Question")
        query_button = gr.Button("Query")
        query_output = gr.Text(label="Query Output")
        query_button.click(perform_query, inputs=question_input, outputs=query_output)

demo.launch()
Categories
Errors

Qdrant AI Research

Not Working

import gradio as gr
import os
import time
import arxiv
from langchain_community.vectorstores import Qdrant
from langchain_community.document_loaders import PyPDFLoader, DirectoryLoader
from langchain_community.chat_models import ChatOllama
from langchain.prompts import ChatPromptTemplate
from langchain.pydantic_v1 import BaseModel
from langchain.schema.output_parser import StrOutputParser
from langchain.schema.runnable import RunnableParallel, RunnablePassthrough
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.embeddings import GPT4AllEmbeddings
from langchain_community.embeddings.ollama import OllamaEmbeddings

# Assuming Qdrant and embeddings setup done globally for simplicity
def init_or_load_qdrant(path="./tmp/local_qdrant", collection_name="arxiv_papers"):
    try:
        qdrant_instance = Qdrant.load(
            path=path,
            collection_name=collection_name,
        )
        print("Existing embeddings loaded.")
    except Exception as e:
        print("No existing embeddings found, starting new Qdrant instance.", e)
        qdrant_instance = None

    return qdrant_instance, qdrant_instance.as_retriever() if qdrant_instance else None

# Attempt to load existing Qdrant instance and retriever
qdrant, retriever = init_or_load_qdrant()

def process_papers(query):
    dirpath = "arxiv_papers"
    if not os.path.exists(dirpath):
        os.makedirs(dirpath)
    
    client = arxiv.Client()
    search = arxiv.Search(
        query=query,
        max_results=5,
        sort_order=arxiv.SortOrder.Descending
    )
    
    for result in client.results(search):
        while True:
            try:
                result.download_pdf(dirpath=dirpath)
                print(result)
                print(f"-> Paper id {result.get_short_id()} with title '{result.title}' is downloaded.")
                break
            except (FileNotFoundError, ConnectionResetError) as e:
                print("Error occurred:", e)
                time.sleep(5)
    
    papers = []
    loader = DirectoryLoader(dirpath, glob="./*.pdf", loader_cls=PyPDFLoader)
    try:
        papers = loader.load()
    except Exception as e:
        print(f"Error loading file: {e}")
    full_text = ''
    for paper in papers:
        full_text += paper.page_content
    
    full_text = " ".join(line for line in full_text.splitlines() if line)
    text_splitter = RecursiveCharacterTextSplitter(chunk_size=500, chunk_overlap=50)
    paper_chunks = text_splitter.create_documents([full_text])
    
    global qdrant, retriever
    qdrant = Qdrant.from_documents(
        documents=paper_chunks,
        embedding=OllamaEmbeddings(model='nomic-embed-text'),
        path="./tmp/local_qdrant",
        collection_name="arxiv_papers",
    )
    retriever = qdrant.as_retriever()
    
    return "Papers processed and saved to embeddings database."

def perform_query(question_text):
    global retriever
    if not retriever:
        return "Error: No papers processed. Please process papers first."
    
    template = """Answer the question based only on the following context:
{context}

Question: {question}
"""
    prompt = ChatPromptTemplate.from_template(template)
    
    ollama_llm = "llama2:7b-chat"
    model = ChatOllama(model=ollama_llm)
    
    chain = (
        RunnableParallel({"context": retriever, "question": RunnablePassthrough()})
        | prompt
        | model
        | StrOutputParser()
    )
    
    class Question(BaseModel):
        __root__: str
    
    chain = chain.with_types(input_type=Question)
    result = chain.invoke(Question(__root__=question_text))
    return result

with gr.Blocks() as demo:
    with gr.Tab("Process Papers"):
        query_input = gr.Text(label="Search Query")
        process_button = gr.Button("Process Papers")
        process_output = gr.Text(label="Process Output")
        process_button.click(process_papers, inputs=query_input, outputs=process_output)

    with gr.Tab("Query Retriever"):
        question_input = gr.Text(label="Question")
        query_button = gr.Button("Query")
        query_output = gr.Text(label="Query Output")
        query_button.click(perform_query, inputs=question_input, outputs=query_output)

demo.launch()
Categories
Errors

Fixing UTF-8 Encoding Surrogate Error in Python Embeddings

Error message

UnicodeEncodeError: 'utf-8' codec can't encode characters in position 100-103: surrogates not allowed

langchain_community/embeddings/gpt4all.py

from typing import Any, Dict, List

from langchain_core.embeddings import Embeddings
from langchain_core.pydantic_v1 import BaseModel, root_validator


class GPT4AllEmbeddings(BaseModel, Embeddings):
    """GPT4All embedding models.

    To use, you should have the gpt4all python package installed

    Example:
        .. code-block:: python

            from langchain_community.embeddings import GPT4AllEmbeddings

            embeddings = GPT4AllEmbeddings()
    """

    client: Any  #: :meta private:

    @root_validator()
    def validate_environment(cls, values: Dict) -> Dict:
        """Validate that GPT4All library is installed."""

        try:
            from gpt4all import Embed4All

            values["client"] = Embed4All()
        except ImportError:
            raise ImportError(
                "Could not import gpt4all library. "
                "Please install the gpt4all library to "
                "use this embedding model: pip install gpt4all"
            )
        return values
    def sanitize_text(self, text):
        return text.encode('utf-8', 'ignore').decode('utf-8')

    def embed_documents(self, texts: List[str]) -> List[List[float]]:
        """Embed a list of documents using GPT4All.

        Args:
            texts: The list of texts to embed.

        Returns:
            List of embeddings, one for each text.
        """
        texts_sanitized = [self.sanitize_text(text) for text in texts]
        embeddings = [self.client.embed(text) for text in texts_sanitized]
        return [list(map(float, e)) for e in embeddings]

    def embed_query(self, text: str) -> List[float]:
        """Embed a query using GPT4All.

        Args:
            text: The text to embed.

        Returns:
            Embeddings for the text.
        """
        return self.embed_documents([text])[0]
Categories
RAG

Ragas Evaluate Embeddings

from llama_index import download_loader, VectorStoreIndex, ServiceContext
from ragas.testset.evolutions import simple, reasoning, multi_context
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from ragas.metrics import context_precision, context_recall
from langchain.embeddings import HuggingFaceEmbeddings
from ragas.llama_index import evaluate
import nest_asyncio
import pandas as pd

# Load documents using llama-hub
SemanticScholarReader = download_loader("SemanticScholarReader")
loader = SemanticScholarReader()
query_space = "large language models"
documents = loader.load_data(query=query_space, limit=100)

# Generator with openai models
generator_llm = ChatOpenAI(model="gpt-3.5-turbo-16k")
critic_llm = ChatOpenAI(model="gpt-4")
embeddings = OpenAIEmbeddings()

generator = TestsetGenerator.from_langchain(
    generator_llm,
    critic_llm,
    embeddings
)

distributions = {
    simple: 0.5,
    multi_context: 0.4,
    reasoning: 0.1
}

# Generate testset
testset = generator.generate_with_llama_index_docs(documents, 100, distributions)
test_df = testset.to_pandas()

# Collect questions and answers
test_questions = test_df['question'].values.tolist()
test_answers = [[item] for item in test_df['answer'].values.tolist()]

nest_asyncio.apply()

def build_query_engine(embed_model):
    vector_index = VectorStoreIndex.from_documents(
        documents, service_context=ServiceContext.from_defaults(chunk_size=512),
        embed_model=embed_model,
    )
    query_engine = vector_index.as_query_engine(similarity_top_k=2)
    return query_engine

# Metrics
metrics = [
    context_precision,
    context_recall,
]

# Evaluate OpenAI embeddings
openai_model = OpenAIEmbeddings()
query_engine1 = build_query_engine(openai_model)
result_openai = evaluate(query_engine1, metrics, test_questions, test_answers)

# Evaluate BGE embeddings
bge_model = HuggingFaceEmbeddings(model_name="BAAI/bge-small-en-v1.5")
query_engine2 = build_query_engine(bge_model)
result_bge = evaluate(query_engine2, metrics, test_questions, test_answers)

# Compare Scores
print("OpenAI embeddings results:", result_openai)
print("BGE embeddings results:", result_bge)

# Export the results to pandas for further analysis
# Assuming `result` is the evaluation result dictionary
result_df_openai = pd.DataFrame([result_openai])
result_df_bge = pd.DataFrame([result_bge])

print(result_df_openai.head())
print(result_df_bge.head())
Categories
RAG

Ragas Evaluate LLM

import os
import pandas as pd
import nest_asyncio
from llama_index import download_loader, SimpleDirectoryReader, VectorStoreIndex, ServiceContext, HuggingFaceInferenceAPI
from llama_index.embeddings import HuggingFaceInferenceAPIEmbedding
from ragas.testset import TestsetGenerator
from ragas.testset.generator import TestsetGenerator
from ragas.testset.evolutions import simple, reasoning, multi_context
from langchain_openai import ChatOpenAI, OpenAIEmbeddings
from datasets import Dataset
from ragas import evaluate
from ragas.metrics import faithfulness, answer_relevancy, answer_correctness
import seaborn as sns
import matplotlib.pyplot as plt

# Setting environment variable (replace 'Your OPEN AI key' with your actual API key)
os.environ['OPENAI_API_KEY'] = 'Your OPEN AI key'

# Loading documents
reader = SimpleDirectoryReader("./arxiv-papers/", num_files_limit=30)
documents = reader.load_data()

# Generator with openai models
generator_llm = ChatOpenAI(model="gpt-3.5-turbo-16k")
critic_llm = ChatOpenAI(model="gpt-4")
embeddings = OpenAIEmbeddings()

generator = TestsetGenerator.from_langchain(
    generator_llm,
    critic_llm,
    embeddings
)

distributions = {
    simple: 0.5,
    multi_context: 0.4,
    reasoning: 0.1
}

# Generate testset
testset = generator.generate_with_llama_index_docs(documents, 100, distributions)
test_df = testset.to_pandas()

# Collect questions and answers
test_questions = test_df['question'].values.tolist()
test_answers = [[item] for item in test_df['answer'].values.tolist()]

nest_asyncio.apply()

def build_query_engine(llm):
    vector_index = VectorStoreIndex.from_documents(
        documents, service_context=ServiceContext.from_defaults(chunk_size=512, llm=llm),
        embed_model=HuggingFaceInferenceAPIEmbedding,
    )

    query_engine = vector_index.as_query_engine(similarity_top_k=2)
    return query_engine

def generate_responses(query_engine, test_questions, test_answers):
    responses = [query_engine.query(q) for q in test_questions]

    answers = []
    contexts = []
    for r in responses:
        answers.append(r.response)
        contexts.append([c.node.get_content() for c in r.source_nodes])
    dataset_dict = {
        "question": test_questions,
        "answer": answers,
        "contexts": contexts,
    }
    if test_answers is not None:
        dataset_dict["ground_truth"] = test_answers
    ds = Dataset.from_dict(dataset_dict)
    return ds

# Evaluating Zephyr 7B Alpha LLM
zephyr_llm = HuggingFaceInferenceAPI(
    model_name="HuggingFaceH4/zephyr-7b-alpha",
    token="Your Hugging Face token"
)
query_engine1 = build_query_engine(zephyr_llm)
result_ds_zephyr = generate_responses(query_engine1, test_questions, test_answers)
result_zephyr = evaluate(
    result_ds_zephyr,
    metrics=[
        faithfulness,
        answer_relevancy,
        answer_correctness,
    ],
)

# Evaluating Falcon-7B-Instruct LLM
falcon_llm = HuggingFaceInferenceAPI(
    model_name="tiiuae/falcon-7b-instruct",
    token="Your Huggingface token"
)
query_engine2 = build_query_engine(falcon_llm)
result_ds_falcon = generate_responses(query_engine2, test_questions, test_answers)
result_falcon = evaluate(
    result_ds_falcon,
    metrics=[
        faithfulness,
        answer_relevancy,
        answer_correctness,
    ],
)

# Comparison analysis
result_zephyr_df = pd.DataFrame([result_zephyr])
result_falcon_df = pd.DataFrame([result_falcon])

def analysis(zephyr_df, falcon_df):
    sns.set_style("whitegrid")
    fig, axs = plt.subplots(1, 3, figsize=(12, 5))
    metrics = ['faithfulness', 'answer_relevancy', 'answer_correctness']
    for i, metric in enumerate(metrics):
        sns.kdeplot(data=[zephyr_df[metric].values, falcon_df[metric].values], legend=False, ax=axs[i], fill=True)
        axs[i].set_title(f'{metric} scores distribution')
        axs[i].legend(labels=["zephyr", "falcon"])
    plt.tight_layout()
    plt.show()

analysis(result_zephyr_df, result_falcon_df)
Categories
AI Agents

AWS AI Agent

NOT TESTED

import logging
import boto3
import time
import yaml
import json
from botocore.exceptions import ClientError

def create_agent(bedrock, agent_name, foundation_model, role_arn, instruction):
    try:
        response = bedrock.create_agent(agentName=agent_name, foundationModel=foundation_model, agentResourceRoleArn=role_arn, instruction=instruction)
        return response["agent"]
    except ClientError as e:
        logging.error(f"Couldn't create agent due to: {e}")
        raise

def create_agent_action_group(bedrock, name, description, agent_id, agent_version, function_arn, api_schema):
    try:
        response = bedrock.create_agent_action_group(actionGroupName=name, description=description, agentId=agent_id, agentVersion=agent_version, actionGroupExecutor={"lambda": function_arn}, apiSchema={"payload": api_schema})
        return response["agentActionGroup"]
    except ClientError as e:
        logging.error(f"Couldn't create agent action group due to: {e}")
        raise

def prepare_agent(bedrock, agent_id):
    try:
        return bedrock.prepare_agent(agentId=agent_id)
    except ClientError as e:
        logging.error(f"Couldn't prepare agent due to: {e}")
        raise

def create_agent_alias(bedrock, name, agent_id):
    try:
        response = bedrock.create_agent_alias(agentAliasName=name, agentId=agent_id)
        return response["agentAlias"]
    except ClientError as e:
        logging.error(f"Couldn't create agent alias due to: {e}")
        raise

def main():
    bedrock = boto3.client(service_name='bedrock-agent', region_name='us-east-1')
    agent_name = 'BusinessGuru'
    foundation_model = 'anthropic.claude-v2'
    role_arn = 'arn:aws:iam::<account-id>:role/<role-name>'
    instruction = 'Your task is to generate insightful and valuable business quotes that inspire entrepreneurs \
                   and business leaders. Focus on themes such as innovation, leadership, perseverance, \
                   strategic thinking, and financial wisdom. Use these themes to craft quotes that offer \
                   motivation, reflect on the challenges and rewards of building and running a business, \
                   and provide guidance for decision-making processes. Ensure the quotes are memorable \
                   and resonate with the entrepreneurial spirit, offering fresh perspectives and wisdom \
                   to support the business community in their journey.'

    # Create the agent
    agent = create_agent(bedrock, agent_name, foundation_model, role_arn, instruction)
    agent_id = agent['agentId']
    print(f"Agent created successfully: {agent_id}")
    
    time.sleep(10)
    
    # Create agent action group
    with open("api_schema.yaml") as file:
        api_schema = json.dumps(yaml.safe_load(file))
    name = "BusinessQuotesGroup"
    description = "Generates Business Quotes"
    agent_version = "DRAFT"
    function_arn = "arn:aws:lambda:us-west-2:123456789012:function:quotesGenerator"
    agentgroup = create_agent_action_group(bedrock, name, description, agent_id, agent_version, function_arn, api_schema)                
    print(agentgroup['actionGroupId'])
    
    time.sleep(5)
    
    # Prepare the agent
    agentprepared = prepare_agent(bedrock, agent_id)                
    print(agentprepared)

    time.sleep(20)
    
    # Create an agent alias
    agentalias = create_agent_alias(bedrock, name, agent_id)
    print(agentalias['agentAliasId'])

if __name__ == "__main__":
    main()