Categories
AI

OpenAI Parallel Function Calling + Node.js

import OpenAI from "openai";
import yahooFinance from 'yahoo-finance2';
import fetch from 'node-fetch';

const openai = new OpenAI();

async function getLatestCompanyNews(company_name) {
    const GNEWS_API_KEY = process.env.GNEWS_API_KEY;
    const url = `https://gnews.io/api/v4/search?q=${company_name}&token=${GNEWS_API_KEY}&lang=en&sortBy=publishedAt`;
    try {
        const response = await fetch(url);
        const data = await response.json();
        return data.articles;
    } catch (error) {
        console.error(`Error fetching news for ${company_name}: ${error}`);
        return [];
    }
}

async function usdToGbp(usd_amount) {
    const url = "https://api.exchangerate-api.com/v4/latest/USD";
    try {
        const response = await fetch(url);
        const data = await response.json();
        const gbp_rate = data.rates['GBP'];
        return usd_amount * gbp_rate;
    } catch (error) {
        console.error(`Error converting USD to GBP: ${error}`);
        return `Error: ${error}`;
    }
}

async function getStockPrice(symbol) {
    try {
        const quote = await yahooFinance.quote(symbol);
        return { 
            symbol: symbol, 
            price: quote.regularMarketPrice, 
            currency: quote.currency 
        };
    } catch (error) {
        console.error(`Error fetching stock price for ${symbol}: ${error}`);
        throw error;
    }
}

const tools = [
    {
        "type": "function",
        "function": {
            "name": "getStockPrice",
            "description": "Get the current stock price of a company using its stock symbol",
            "parameters": {
                "type": "object",
                "properties": {
                    "symbol": {
                        "type": "string",
                        "description": "Stock symbol (e.g., 'AAPL' for Apple)"
                    }
                },
                "required": ["symbol"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "getLatestCompanyNews",
            "description": "Get the latest news about a company",
            "parameters": {
                "type": "object",
                "properties": {
                    "company_name": {
                        "type": "string",
                        "description": "Company name (e.g., 'Apple')"
                    }
                },
                "required": ["company_name"]
            }
        }
    },
    {
        "type": "function",
        "function": {
            "name": "usdToGbp",
            "description": "Convert USD to GBP",
            "parameters": {
                "type": "object",
                "properties": {
                    "usd_amount": {
                        "type": "number",
                        "description": "Amount in USD"
                    }
                },
                "required": ["usd_amount"]
            }
        }
    },
];

// Step 1: Define your assistant 
const assistant = await openai.beta.assistants.create({
    name: "Data Analyst",
    instructions:
        "You are a Data Analyst",
    tools: tools,
    model: "gpt-4-1106-preview",
});

// Step 2: Creating a thread and sending a message
const thread = await openai.beta.threads.create();

// Step 3: Create a message
const message = await openai.beta.threads.messages.create(thread.id, {
    role: "user",
    content: "Can you please provide me stock price, " + 
             "stock price in GBP, " +
             "and the latest company news of Apple?"
});

// Step 4: Create a run with custom instructions
const run = await openai.beta.threads.runs.create(thread.id, {
    assistant_id: assistant.id,
    instructions: "Please address the user as Mervin Praison.",    
});

// Function to check run status and print messages
const checkStatusAndPrintMessages = async (threadId, runId) => {
    let runStatus = await openai.beta.threads.runs.retrieve(threadId, runId);
    console.log(runStatus)    
    if(runStatus.status === "completed"){
        let messages = await openai.beta.threads.messages.list(threadId);
        messages.data.forEach((msg) => {
            const role = msg.role;
            const content = msg.content[0].text.value; 
            console.log(
                `${role.charAt(0).toUpperCase() + role.slice(1)}: ${content}`
            );
        });
        console.log("Run is completed.");
        clearInterval(intervalId);
    } else if (runStatus.status === 'requires_action') {
        console.log("Requires action");
        const requiredActions = runStatus.required_action.submit_tool_outputs.tool_calls;
        console.log(requiredActions);
        
        // Dispatch table
        const dispatchTable = {
            "getStockPrice": getStockPrice,
            "getLatestCompanyNews": getLatestCompanyNews,
            "usdToGbp": usdToGbp
        };

        let toolsOutput = [];
    
        for (const action of requiredActions) {
            const funcName = action.function.name;
            const functionArguments = JSON.parse(action.function.arguments);

            if (dispatchTable[funcName]) {
                try {
                    const output = await dispatchTable[funcName](...Object.values(functionArguments));
                    toolsOutput.push({ tool_call_id: action.id, output: JSON.stringify(output) });
                } catch (error) {
                    console.log(`Error executing function ${funcName}: ${error}`);
                }
            } else {
                console.log("Function not found");
            }
        }

        await openai.beta.threads.runs.submitToolOutputs(threadId, runId, { tool_outputs: toolsOutput });
    } else {
        console.log("Run is not completed yet.");
    }  
};

const intervalId = setInterval(() => {
    checkStatusAndPrintMessages(thread.id, run.id)
}, 10000);

package.json

{
  "dependencies": {
    "node-fetch": "^3.3.2",
    "openai": "^4.16.1",
    "yahoo-finance2": "^2.8.1"
  },
  "type": "module"
}
Categories
AI

OpenAI ChatGPT RAG Retrieval Augmented Generation Example

import pandas as pd
import numpy as np
from ast import literal_eval
import openai

def get_embedding(text: str, model="text-similarity-davinci-001", **kwargs):
    text = text.replace("\n", " ")
    response = openai.embeddings.create(input=[text], model=model, **kwargs)
    return response.data[0].embedding

def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

# Load the dataset and convert embeddings
datafile_path = "fine_food_reviews_with_embeddings.csv"
df = pd.read_csv(datafile_path)
df["embedding"] = df.embedding.apply(literal_eval).apply(np.array)

# Function to search through the reviews
def search_reviews(df, product_description, n=3, pprint=True):
    product_embedding = get_embedding(
        product_description,
        model="text-embedding-ada-002"
    )
    df["similarity"] = df.embedding.apply(lambda x: cosine_similarity(x, product_embedding))

    results = (
        df.sort_values("similarity", ascending=False)
        .head(n)
        .combined.str.replace("Title: ", "")
        .str.replace("; Content:", ": ")
    )
    if pprint:
        for r in results:
            print(r[:200])
            print()
    return results

client = openai.OpenAI()

def generate_answer_with_chat(context, question):
    conversation = [
        {"role": "system", "content": "You are a knowledgeable assistant."},
        {"role": "user", "content": f"Context: {context}"},
        {"role": "user", "content": f"Question: {question}"}
    ]

    stream = client.chat.completions.create(
        model="gpt-4",
        messages=conversation,
        stream=True,
    )

    response_text = ""
    for chunk in stream:
        if chunk.choices[0].delta.content is not None:
            response_text += chunk.choices[0].delta.content

    return response_text

def rag_search(df, query, n=3):
    # Retrieve top N similar reviews
    top_reviews = search_reviews(df, query, n=n, pprint=False)

    # Combine the top reviews into a single context string
    context = " ".join(top_reviews)

    # Generate an answer based on the context using chat API
    answer = generate_answer_with_chat(context, query)

    return answer

# Example usage
answer = rag_search(df, "What do people think about the texture of whole wheat pasta?")
print(answer)

Embedding

Search Embedding

Categories
AI

OpenAI ChatGPT Embedding Search Example

import pandas as pd
import numpy as np
from ast import literal_eval
import openai

def get_embedding(text: str, model="text-similarity-davinci-001", **kwargs):
    text = text.replace("\n", " ")
    response = openai.embeddings.create(input=[text], model=model, **kwargs)
    return response.data[0].embedding

def cosine_similarity(a, b):
    return np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b))

# Load the dataset and convert embeddings
datafile_path = "fine_food_reviews_with_embeddings.csv"
df = pd.read_csv(datafile_path)
df["embedding"] = df.embedding.apply(literal_eval).apply(np.array)

# Function to search through the reviews
def search_reviews(df, product_description, n=3, pprint=True):
    product_embedding = get_embedding(
        product_description,
        model="text-embedding-ada-002"
    )
    df["similarity"] = df.embedding.apply(lambda x: cosine_similarity(x, product_embedding))

    results = (
        df.sort_values("similarity", ascending=False)
        .head(n)
        .combined.str.replace("Title: ", "")
        .str.replace("; Content:", ": ")
    )
    if pprint:
        for r in results:
            print(r[:200])
            print()
    return results

# Example searches
results = search_reviews(df, "tomato", n=1)

Categories
AI

OpenAI ChatGPT Embedding Example

import pandas as pd
from openai import OpenAI

# Initialize OpenAI client
client = OpenAI()

def get_embedding(text, model="text-embedding-ada-002"):
    text = text.replace("\n", " ")
    return client.embeddings.create(input=[text], model=model).data[0].embedding

# Load preprocessed dataset
df = pd.read_csv("fine_food_reviews.csv", index_col=0)

# Create the 'combined' column by concatenating 'Summary' and 'Text'
df['combined'] = "Title: " + df['Summary'].str.strip() + "; Content: " + df['Text'].str.strip()

# Get embeddings and save them for future reuse
df["embedding"] = df["combined"].apply(lambda x: get_embedding(x, model='text-embedding-ada-002'))
df.to_csv("fine_food_reviews_with_embeddings.csv")
Categories
AI

Fine Tuning GPT 3.5 16K

Fine Tuning

from openai import OpenAI
import time

client = OpenAI()

# Create file for fine-tuning
file = client.files.create(
    file=open("mervinpraison.jsonl", "rb"),
    purpose="fine-tune"
)

print(f"File ID: {file.id}, File Status: {file.status}")

# Create a fine-tuning job
job = client.fine_tuning.jobs.create(
    training_file=file.id,
    model="gpt-3.5-turbo-1106"
)

print(f"Job ID: {job.id}, Job Status: {job.status}")

while True:
    time.sleep(10)  
    job = client.fine_tuning.jobs.retrieve(job.id)
    if job.status == 'succeeded':
        model_name = f"ft-{job.model}:suffix:{job.id}"
        print(f"Job ID: {job.id}, Final Job Status: {job.status}")
        print(f"Model Name: {model_name}")
        break
    elif job.status == 'failed':
        print(f"Job ID: {job.id}, Final Job Status: {job.status}")
        break
    else:
        print(f"Checking... Job ID: {job.id}, Current Job Status: {job.status}")

Using Fine tuned Model

from openai import OpenAI
client = OpenAI()

response = client.chat.completions.create(
    model="ft:gpt-3.5-turbo-1106:mervin-praison::8QUpHZjq",
    messages=[
        {"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."},
        {"role": "user", "content": "How far is the sun?"},
    ]
)

print(response.choices[0].message.content)

Using Fine tuned Model with UI

from openai import OpenAI
import gradio as gr
client = OpenAI()

def chat_with_model(user_input):
    response = client.chat.completions.create(
        model="ft:gpt-3.5-turbo-1106:mervin-praison::8QVebIU4",
        messages=[
            {"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."},
            {"role": "user", "content": user_input},
        ]
    )
    return response.choices[0].message.content

chatbot = gr.Interface(
    fn=chat_with_model,
    inputs=gr.Textbox(lines=2, placeholder="Enter your question here...", label="Question"),
    outputs="text",
    title="Chat with Mervin Praison Chatbot",
    description="This is a chatbot with a sense of humor. Ask any question!"
)

chatbot.launch()

mervinpraison.jsonl

{"messages": [{"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."}, {"role": "user", "content": "What's the tallest mountain in the world?"}, {"role": "assistant", "content": "Mount Everest, it's so high it's almost touching the sky!"}]}
{"messages": [{"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."}, {"role": "user", "content": "What is the speed of light?"}, {"role": "assistant", "content": "It's about 299,792 kilometers per second. Faster than any of us on a Monday morning!"}]}
{"messages": [{"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."}, {"role": "user", "content": "Who discovered gravity?"}, {"role": "assistant", "content": "That would be Sir Isaac Newton. An apple a day keeps the gravity at play!"}]}
{"messages": [{"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."}, {"role": "user", "content": "How many planets are in the Solar System?"}, {"role": "assistant", "content": "Eight planets. Sorry Pluto, you're still not on the list!"}]}
{"messages": [{"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."}, {"role": "user", "content": "What is the largest ocean?"}, {"role": "assistant", "content": "The Pacific Ocean. It's so big, you can't see the other side from here!"}]}
{"messages": [{"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."}, {"role": "user", "content": "What's the smallest country in the world?"}, {"role": "assistant", "content": "Vatican City. It's so small, you might miss it if you blink!"}]}
{"messages": [{"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."}, {"role": "user", "content": "Who is the author of 'Harry Potter'?"}, {"role": "assistant", "content": "J.K. Rowling. She cast a spell and created a whole new world!"}]}
{"messages": [{"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."}, {"role": "user", "content": "What is the boiling point of water?"}, {"role": "assistant", "content": "100 degrees Celsius. It's hot enough to make a cup of tea, or two!"}]}
{"messages": [{"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."}, {"role": "user", "content": "What's the longest river in the world?"}, {"role": "assistant", "content": "The Nile River. It's so long, it could be a marathon route!"}]}
{"messages": [{"role": "system", "content": "Mervin Praison is a factual chatbot with a sense of humor."}, {"role": "user", "content": "Who was the first person on the moon?"}, {"role": "assistant", "content": "Neil Armstrong. He took a small step for man and a giant leap for mankind (and chatbots!)" }]}
Categories
AI

AutoGen Chess Agent

import chess, chess.svg, autogen, sys, os
from collections import defaultdict
from typing import Any, Dict, List, Optional, Union

sys_msg = """You are an AI-powered chess board agent. 
Translate natural language into legal UCI moves. 
Reply only with a UCI move string."""

class RefereeAgent(autogen.AssistantAgent):
    board: chess.Board
    correct_move_messages: Dict[autogen.Agent, List[Dict]]

    def __init__(self, board: chess.Board):
        super().__init__( name="RefereeAgent", system_message=sys_msg, llm_config={"temperature": 0.0}, max_consecutive_auto_reply=10, )
        self.register_reply(autogen.ConversableAgent, RefereeAgent._generate_board_reply)
        self.board = board
        self.correct_move_messages = defaultdict(list)

    def _generate_board_reply( self, messages: Optional[List[Dict]] = None, sender: Optional[autogen.Agent] = None, config: Optional[Any] = None, ) -> Union[str, Dict, None]:
        message = messages[-1]
        reply = self.generate_reply(self.correct_move_messages[sender] + [message], sender, exclude=[RefereeAgent._generate_board_reply])
        uci_move = reply if isinstance(reply, str) else str(reply["content"])
        try:
            self.board.push_uci(uci_move)
        except ValueError as e:
            return True, f"Error: {e}"
        else:
            print("Current board state:")
            print(self.board)
            self.correct_move_messages[sender].extend([message, self._message_to_dict(uci_move)])
            self.correct_move_messages[sender][-1]["role"] = "assistant"
            
            # Call the function to save the SVG
            self.save_board_state_as_svg()

            return True, uci_move
    
    def save_board_state_as_svg(self):
        moves_dir = 'moves'
        if not os.path.exists(moves_dir):
            os.makedirs(moves_dir)
        move_count = len(self.board.move_stack)
        svg_filename = os.path.join(moves_dir, f'move_{move_count}.svg')
        svg_output = chess.svg.board(self.board)
        with open(svg_filename, 'w') as file:
            file.write(svg_output)

sys_msg_tmpl = """Your name is {name} and you are a chess player.
You are playing against {opponent_name}.
You are playing as {color}.
You communicate your move using universal chess interface language.
You also chit-chat with your opponent when you communicate to light up the mood.
You should ensure both you and the opponent are making legal moves.
Do not apologise for making illegal moves."""

class ChessPlayerAgent(autogen.AssistantAgent):
    def __init__( self, color: str, referee_agent: RefereeAgent, max_turns: int, **kwargs, ):
        if color not in ["white", "black"]:
            raise ValueError(f"color must be either white or black, but got {color}")
        opponent_color = "black" if color == "white" else "white"
        name = f"Player {color}"
        opponent_name = f"Player {opponent_color}"
        sys_msg = sys_msg_tmpl.format( name=name, opponent_name=opponent_name, color=color, )
        super().__init__(name=name,system_message=sys_msg,max_consecutive_auto_reply=max_turns,**kwargs,)
        self.register_reply(ChessPlayerAgent, ChessPlayerAgent._generate_reply_for_player, config=referee_agent)        
        self.register_reply(RefereeAgent, ChessPlayerAgent._generate_reply_for_board, config=referee_agent.board)
        self.update_max_consecutive_auto_reply(referee_agent.max_consecutive_auto_reply(), referee_agent)

    def _generate_reply_for_player( self, messages: Optional[List[Dict]] = None, sender: Optional[autogen.Agent] = None, config: Optional[RefereeAgent] = None, ) -> Union[str, Dict, None]:
        referee_agent = config
        board_state_msg = [{"role": "system", "content": f"Current board:\n{referee_agent.board}"}]
        # propose a reply which will be sent to the board agent for verification.
        
        message = self.generate_reply(messages + board_state_msg, sender, exclude=[ChessPlayerAgent._generate_reply_for_player])
        if message is None:
            return True, None
        # converse with the board until a legal move is made or max allowed retries.
        self.initiate_chat(referee_agent, clear_history=False, message=message, silent=self.human_input_mode == "NEVER")
        last_message = self._oai_messages[referee_agent][-1]
        if last_message["role"] == "assistant":
            sys.stdout.write(f"{self.name}: I yield.\n")
            return True, None
        return True, self._oai_messages[referee_agent][-2]

    def _generate_reply_for_board( self, messages: Optional[List[Dict]] = None, sender: Optional[autogen.Agent] = None, config: Optional[chess.Board] = None, ) -> Union[str, Dict, None]:
        board = config
        board_state_msg = [{"role": "system", "content": f"Current board:\n{board}"}]
        last_message = messages[-1]
        if last_message["content"].startswith("Error"):
            last_message["role"] = "system"
            return True, self.generate_reply(messages + board_state_msg, sender, exclude=[ChessPlayerAgent._generate_reply_for_board])
        else:
            return True, None

"""## Construct Agents"""

max_turn = 2

board = chess.Board()
referee_agent = RefereeAgent(board=board)
player_black = ChessPlayerAgent(
    color="black",
    referee_agent=referee_agent,
    max_turns=max_turn,
    llm_config={"temperature": 0.0, "seed": 48},
)
player_white = ChessPlayerAgent(
    color="white",
    referee_agent=referee_agent,
    max_turns=max_turn,
    llm_config={"temperature": 0.0, "seed": 49},
)

"""## Start Game"""

player_black.initiate_chat(player_white, message="Your turn.")

Same code with few extras

import chess
import chess.svg
import autogen
import sys
import os

from collections import defaultdict
from typing import Any, Dict, List, Optional, Union

sys_msg = """You are an AI-powered chess board agent.
You translate the user's natural language input into legal UCI moves.
You should only reply with a UCI move string extracted from the user's input."""

class UmpireAgent(autogen.AssistantAgent):
    board: chess.Board
    correct_move_messages: Dict[autogen.Agent, List[Dict]]

    def __init__(self, board: chess.Board):
        super().__init__(
            name="UmpireAgent",
            system_message=sys_msg,
            llm_config={"temperature": 0.0},
            max_consecutive_auto_reply=10,
        )
        self.register_reply(autogen.ConversableAgent, UmpireAgent._generate_board_reply)
        self.board = board
        self.correct_move_messages = defaultdict(list)

    def _generate_board_reply(
        self,
        messages: Optional[List[Dict]] = None,
        sender: Optional[autogen.Agent] = None,
        config: Optional[Any] = None,
    ) -> Union[str, Dict, None]:
        message = messages[-1]
        reply = self.generate_reply(self.correct_move_messages[sender] + [message], sender, exclude=[UmpireAgent._generate_board_reply])
        uci_move = reply if isinstance(reply, str) else str(reply["content"])
        try:
            self.board.push_uci(uci_move)
        except ValueError as e:
            return True, f"Error: {e}"
        else:
            print("Current board state:")
            print(self.board)
            self.correct_move_messages[sender].extend([message, self._message_to_dict(uci_move)])
            self.correct_move_messages[sender][-1]["role"] = "assistant"
            
            # Save the SVG to a file in 'moves' folder
            moves_dir = 'moves'
            if not os.path.exists(moves_dir):
                os.makedirs(moves_dir)
            move_count = len(self.board.move_stack)
            svg_filename = os.path.join(moves_dir, f'move_{move_count}.svg')
            svg_output = chess.svg.board(self.board)
            with open(svg_filename, 'w') as file:
                file.write(svg_output)

            return True, uci_move

sys_msg_tmpl = """Your name is {name} and you are a chess player.
You are playing against {opponent_name}.
You are playing as {color}.
You communicate your move using universal chess interface language.
You also chit-chat with your opponent when you communicate a move to light up the mood.
You should ensure both you and the opponent are making legal moves.
Do not apologize for making illegal moves."""

class ChessPlayerAgent(autogen.AssistantAgent):

    def __init__(
        self,
        color: str,
        umpire_agent: UmpireAgent,
        max_turns: int,
        **kwargs,
    ):
        if color not in ["white", "black"]:
            raise ValueError(f"color must be either white or black, but got {color}")
        opponent_color = "black" if color == "white" else "white"
        name = f"Player {color}"
        opponent_name = f"Player {opponent_color}"
        sys_msg = sys_msg_tmpl.format(
            name=name,
            opponent_name=opponent_name,
            color=color,
        )
        super().__init__(
            name=name,
            system_message=sys_msg,
            max_consecutive_auto_reply=max_turns,
            **kwargs,
        )
        self.register_reply(UmpireAgent, ChessPlayerAgent._generate_reply_for_board, config=umpire_agent.board)
        self.register_reply(ChessPlayerAgent, ChessPlayerAgent._generate_reply_for_player, config=umpire_agent)
        self.update_max_consecutive_auto_reply(umpire_agent.max_consecutive_auto_reply(), umpire_agent)

    def _generate_reply_for_board(
        self,
        messages: Optional[List[Dict]] = None,
        sender: Optional[autogen.Agent] = None,
        config: Optional[chess.Board] = None,
    ) -> Union[str, Dict, None]:
        board = config
        # add a system message about the current state of the board.
        board_state_msg = [{"role": "system", "content": f"Current board:\n{board}"}]
        last_message = messages[-1]
        if last_message["content"].startswith("Error"):
            # try again
            last_message["role"] = "system"
            return True, self.generate_reply(messages + board_state_msg, sender, exclude=[ChessPlayerAgent._generate_reply_for_board])
        else:
            return True, None

    def _generate_reply_for_player(
        self,
        messages: Optional[List[Dict]] = None,
        sender: Optional[autogen.Agent] = None,
        config: Optional[UmpireAgent] = None,
    ) -> Union[str, Dict, None]:
        umpire_agent = config
        # add a system message about the current state of the board.
        board_state_msg = [{"role": "system", "content": f"Current board:\n{umpire_agent.board}"}]
        # propose a reply which will be sent to the board agent for verification.
        message = self.generate_reply(messages + board_state_msg, sender, exclude=[ChessPlayerAgent._generate_reply_for_player])
        if message is None:
            return True, None
        # converse with the board until a legal move is made or max allowed retries.
        # change silent to False to see that conversation.
        self.initiate_chat(umpire_agent, clear_history=False, message=message, silent=self.human_input_mode == "NEVER")
        # last message sent by the board agent
        last_message = self._oai_messages[umpire_agent][-1]
        if last_message["role"] == "assistant":
            # didn't make a legal move after a limit times of retries.
            sys.stdout.write(f"{self.name}: I yield.\n")
            return True, None
        return True, self._oai_messages[umpire_agent][-2]

"""## Construct Agents"""

max_turn = 2

board = chess.Board()
umpire_agent = UmpireAgent(board=board)
player_black = ChessPlayerAgent(
    color="black",
    umpire_agent=umpire_agent,
    max_turns=max_turn,
    llm_config={"temperature": 0.5, "cache_seed": 4},
)
player_white = ChessPlayerAgent(
    color="white",
    umpire_agent=umpire_agent,
    max_turns=max_turn,
    llm_config={"temperature": 0.5, "cache_seed": 5},
)

"""## Start Game"""

player_black.initiate_chat(player_white, message="Your turn.")

Categories
AI

Ollama LlamaIndex

Basic

from llama_index.llms import Ollama

# Initialize the model
llm = Ollama(model="mistral")

response = llm.complete("Write a cover letter.")

print(response)

Basic Streaming

from llama_index.llms import Ollama

# Initialize the model
llm = Ollama(model="mistral")

response = llm.stream_complete("Write a cover letter.")

for r in response:
    print(r.delta, end="")

Chat

from llama_index.llms import Ollama
from llama_index.llms import ChatMessage

# Initialize the model
llm = Ollama(model="mistral")

messages = [
    ChatMessage(
        role="system", content="You are helpful assistant to create programs"
    ),
    ChatMessage(role="user", content="Write a python program to calculate the sum of two numbers"),
]

response = llm.chat(messages)
print(response)

Chat Streaming

from llama_index.llms import Ollama
from llama_index.llms import ChatMessage

# Initialize the model
llm = Ollama(model="mistral")

messages = [
    ChatMessage(
        role="system", content="You are helpful assistant to create programs"
    ),
    ChatMessage(role="user", content="Write a python program to calculate the sum of two numbers"),
]

response = llm.stream_chat(messages)
for r in response:
    print(r.delta, end="")
Categories
AI

Langchain ElevenLabs

Basic

from langchain.tools import ElevenLabsText2SpeechTool

text_to_speak = "Hello world! I am Mervin Praison"

tts = ElevenLabsText2SpeechTool()
speech_file = tts.run(text_to_speak)
tts.play(speech_file)

Interface

from langchain.tools import ElevenLabsText2SpeechTool
import gradio as gr

tts = ElevenLabsText2SpeechTool()

def generate_speech(text_to_speak):
    speech_file = tts.run(text_to_speak)
    return speech_file

demo = gr.Interface(
    fn=generate_speech,
    inputs=gr.Textbox(label="Enter Text"),
    outputs=gr.Audio(label="Generated Speech"),
)

if __name__ == "__main__":
    demo.launch()
Categories
AI

Langchain Elevenlabs Agents

from langchain.agents import AgentType, initialize_agent, load_tools
from langchain.llms import OpenAI
from langchain.tools import ElevenLabsText2SpeechTool

# Initialize ElevenLabsText2SpeechTool
tts = ElevenLabsText2SpeechTool()

# Initialize LLM and agent
llm = OpenAI(temperature=0)
tools = load_tools(["eleven_labs_text2speech"])
agent = initialize_agent(
    tools=tools,
    llm=llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True
)

# Request a joke and get the path to the audio file
response = agent.run("Please provide me with an audio file containing a joke about a tiger")
print(response)

tts.play(response)
Categories
AI

LangChain GPT4 Vision API

Basic

from langchain.chat_models import ChatOpenAI
from langchain.schema.messages import HumanMessage, SystemMessage

chat = ChatOpenAI(model="gpt-4-vision-preview", max_tokens=256)
chat.invoke(
    [
        HumanMessage(
            content=[
                {"type": "text", "text": "What is this image showing"},
                {
                    "type": "image_url",
                    "image_url": {
                        "url": "https://raw.githubusercontent.com/langchain-ai/langchain/master/docs/static/img/langchain_stack.png",
                        "detail": "auto",
                    },
                },
            ]
        )
    ]
)

Webcam capture and analyse

import cv2
import time
import base64
from langchain.chat_models import ChatOpenAI
from langchain.schema.messages import HumanMessage, SystemMessage

def encode_image(image_path):
    with open(image_path, "rb") as image_file:
        return base64.b64encode(image_file.read()).decode('utf-8')

cap = cv2.VideoCapture(2)

while True:
    ret, frame = cap.read()
    if not ret:
        print("Failed to grab frame")
        break

    filename = 'image.png'
    cv2.imwrite(filename, frame)

    base64_image = encode_image(filename)
    
    chat = ChatOpenAI(model="gpt-4-vision-preview", max_tokens=256)

    output = chat.invoke(
        [
            HumanMessage(
                content=[
                    {
                        "type": "text", 
                        "text": "Narrate in one line. Don't repeat. Focus on Details"
                    },
                    {
                        "type": "image_url",
                        "image_url": f"data:image/png;base64,{base64_image}"
                    },
                ]
            )
        ]
    )

    print(output)

    time.sleep(2)

cap.release()
cv2.destroyAllWindows()