Categories
API

Google Gemini API Python Example

import vertexai
from vertexai.preview.generative_models import GenerativeModel, Part

model = GenerativeModel("gemini-pro")
responses = model.generate_content(
    """Write a meal plan for today""",
    generation_config={
        "max_output_tokens": 2048,
        "temperature": 0.9,
        "top_p": 1
    },
    stream=True,
)

for response in responses:
    print(response.candidates[0].content.parts[0].text)

Gradio UI

import gradio as gr
import vertexai
from vertexai.preview.generative_models import GenerativeModel, Part

def generate(prompt):
    model = GenerativeModel("gemini-pro")
    response = model.generate_content(
        prompt,
        generation_config={
            "max_output_tokens": 2048,
            "temperature": 0.9,
            "top_p": 1
        },
        stream=False,
    )
    output = response.candidates[0].content.parts[0].text
    return output

iface = gr.Interface(
    fn=generate,
    inputs="text",
    outputs="markdown",
    title="Gemini Pro API UI"
)

iface.launch()
Categories
API

Mistral AI API

export MISTRAL_API_KEY=xxxxxxxxxxxxxxx
curl --location "https://api.mistral.ai/v1/models" \
     --header 'Accept: application/json' \
     --header "Authorization: Bearer $MISTRAL_API_KEY"
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
import os

api_key = os.environ["MISTRAL_API_KEY"]
model = "mistral-small" # Use "mistral-tiny" for "Mistral-7B-v0.2"

client = MistralClient(api_key=api_key)

messages = [
    ChatMessage(role="user", content="Give me a meal plan for today")
]

# No streaming
chat_response = client.chat(
    model=model,
    messages=messages,
)
print(chat_response.choices[0].message.content)

# With streaming
for chunk in client.chat_stream(model=model, messages=messages):
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")
import gradio as gr
from mistralai.client import MistralClient
from mistralai.models.chat_completion import ChatMessage
import os

def chat_with_mistral(user_input):
    api_key = os.environ["MISTRAL_API_KEY"]
    model = "mistral-large-latest"  # Use "Mistral-7B-v0.2" for "mistral-tiny"

    client = MistralClient(api_key=api_key)
    messages = [ChatMessage(role="user", content=user_input)]

    chat_response = client.chat(model=model, messages=messages)
    return chat_response.choices[0].message.content

iface = gr.Interface(
    fn=chat_with_mistral,
    inputs=gr.components.Textbox(label="Enter Your Message"),
    outputs=gr.components.Markdown(label="Chatbot Response"),
    title="Mistral AI Chatbot",
    description="Interact with the Mistral API via this chatbot. Enter a message and get a response.",
    examples=[["Give me a meal plan for today"]],
    allow_flagging="never"
)

iface.launch()
Categories
AI

MLX Stable Diffusion UI

import gradio as gr
from PIL import Image
import numpy as np
import mlx.core as mx
from stable_diffusion import StableDiffusion

def generate_images(prompt, n_images=4, steps=50, cfg=7.5, negative_prompt="", n_rows=1):
    sd = StableDiffusion()

    # Generate the latent vectors using diffusion
    latents = sd.generate_latents(
        prompt,
        n_images=n_images,
        cfg_weight=cfg,
        num_steps=steps,
        negative_text=negative_prompt,
    )
    for x_t in latents:
        mx.simplify(x_t)
        mx.simplify(x_t)
        mx.eval(x_t)

    # Decode them into images
    decoded = []
    for i in range(0, n_images):
        decoded_img = sd.decode(x_t[i:i+1])
        mx.eval(decoded_img)
        decoded.append(decoded_img)

    # Arrange them on a grid
    x = mx.concatenate(decoded, axis=0)
    x = mx.pad(x, [(0, 0), (8, 8), (8, 8), (0, 0)])
    B, H, W, C = x.shape
    x = x.reshape(n_rows, B // n_rows, H, W, C).transpose(0, 2, 1, 3, 4)
    x = x.reshape(n_rows * H, B // n_rows * W, C)
    x = (x * 255).astype(mx.uint8)

    # Convert to PIL Image
    return Image.fromarray(x.__array__())

iface = gr.Interface(
    fn=generate_images,
    inputs=[
        gr.Textbox(label="Prompt"),
        gr.Slider(minimum=1, maximum=10, step=1, value=4, label="Number of Images"),
        gr.Slider(minimum=20, maximum=100, step=1, value=50, label="Steps"),
        gr.Slider(minimum=0.0, maximum=10.0, step=0.1, value=7.5, label="CFG Weight"),
        gr.Textbox(default="", label="Negative Prompt"),
        gr.Slider(minimum=1, maximum=10, step=1, value=1, label="Number of Rows")
    ],
    outputs="image",
    title="Stable Diffusion Image Generator",
    description="Generate images from a textual prompt using Stable Diffusion"
)

iface.launch()
Categories
AutoGen

AutoGen AutoBuilder

import autogen
from autogen.agentchat.contrib.agent_builder import AgentBuilder

# 1. Configuration
config_path = 'OAI_CONFIG_LIST.json'
config_list = autogen.config_list_from_json(config_path)
default_llm_config = {'temperature': 0}

# 2. Initialising Builder
builder = AgentBuilder(config_path=config_path)

# 3. Building agents
building_task = "Find a paper on arxiv by programming, and analyze its application in some domain..."
agent_list, agent_configs = builder.build(building_task, default_llm_config)

# 4. Multi-agent group chat
group_chat = autogen.GroupChat(agents=agent_list, messages=[], max_round=12)
manager = autogen.GroupChatManager(groupchat=group_chat, llm_config={"config_list": config_list, **default_llm_config})
agent_list[0].initiate_chat(
    manager, 
    message="Find a recent paper about gpt-4 on arxiv..."
)

OAI_CONFIG_LIST.json

[
  {
    "model": "gpt-4-1106-preview"
  }
]
Categories
TaskWeaver

TaskWeaver + Text Generation Web UI

Text Generation Web UI

git clone https://github.com/oobabooga/text-generation-webui
cd text-generation-webui
bash start_macos.sh --api --listen --extensions openai
TheBloke/Orca-2-7B-GGUF
orca-2-7b.Q4_K_M.gguf

TaskWeaver

git clone https://github.com/microsoft/TaskWeaver.git
cd TaskWeaver
conda create -n taskweaver python=3.11
conda activate taskweaver
pip install -r requirements.txt

Config

{
  "llm.api_base": "http://0.0.0.0:5000/v1",
  "llm.api_key": "null",
  "llm.model": "orca2",
  "llm.response" : "text"
}
Categories
TaskWeaver

TaskWeaver Stock Price Plugin

stock_price.py

import yfinance as yf
from taskweaver.plugin import Plugin, register_plugin

# Define the detailed prompt for the plugin
stock_price_prompt = r"""
This plugin is designed to fetch real-time stock market data. It performs the following tasks:
- Retrieves the latest stock price information based on the provided stock symbol.
- Extracts key financial details such as the current market price.
- Finally, it returns this information in a structured dictionary format, as shown below:
  {
    "symbol": <stock_symbol>,
    "current_price": <current_market_price>
  }
"""

@register_plugin
class StockPricePlugin(Plugin):
    def __call__(self, stock_symbol: str):
        # Fetch stock data using yfinance
        stock = yf.Ticker(stock_symbol)

        # Check if stock exists and has info
        if not stock.info:
            return {"error": f"No data found for symbol: {stock_symbol}"}

        # Extract the current stock price
        current_price = stock.history(period="1d")['Close'].iloc[-1]

        # Check if current price is available
        if current_price is None:
            return {"error": f"Current price not available for symbol: {stock_symbol}"}

        # Return a dictionary containing the stock symbol and its current price
        return {
            "symbol": stock_symbol,
            "current_price": current_price
        }

stock_price.yaml

name: stock_price
enabled: true
required: false
description: >-
  The StockPricePlugin connects to the Yahoo Finance API using yfinance to fetch
  real-time stock market data. It retrieves and returns the latest stock price
  information based on the provided stock symbol, including the current market price.

parameters:
  - name: stock_symbol
    type: str
    required: true
    description: The stock symbol for which the current market price is to be retrieved.

returns:
  - name: stock_data
    type: dict
    description: >-
      A dictionary containing the stock symbol and its current market price. The format is:
      {
        "symbol": <stock_symbol>,
        "current_price": <current_market_price>
      }
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")