Use ollama to run a ChatGPT-like chat bot on a Mac

Use ollama to run a ChatGPT-like chat bot on a Mac

Ever wanted to experiment with ChatGPT or Gemini/Bard but are slightly afraid of the information (like code) or questions you'd be sharing? Well, there's good news! You can run open source models on a modern M1-based Mac if it has at least 16GB of RAM.


Step 1: Install Ollama (175MB download) : https://ollama.com/download


Step 2: Open/extract the Ollama-darwin.zip file that downloads, and run Ollama .app:

First run of ollama.


Step 3: Let it install the command line:

Install the command line.


Step 4: Click through the Finish screen. Don't copy out the first run command, because I want us to experiment with llama3.


Step 5: Open up your terminal of choice, and lets run ollama for the first time:

ollama run llama3        
Ollama needs to download a model on first run of that model.

If you don't have the model, it will need to download close to 5GB of model data to get started.


Step 6: Chat! But here is something that's possible with llama3 (the latest open source model from Meta/Facebook):

Let's experiment by asking it to write a Python script with almost human language description.


And here is what is produced (almost immediately on a 14" MBP M1 Pro w/ 16GB RAM):

Here's the Python script that fulfills your requirements:

```Python
import sys
from typing import List

def validate_parameters(start: int, amount: int) -> bool:
    if start < 1 or start > 1000:
        return False
    if amount < 1 or amount > 10:
        return False
    return True


def fibonacci_sequence(start: int, amount: int) -> List[int]:
    sequence = [start]
    a, b = start, start + 1
    while len(sequence) < amount:
        a, b = b, a + b
        if b > 1000:
            break
        sequence.append(b)
    return sequence


def main():
    if len(sys.argv) != 3:
        print("Usage: python script.py <start> <amount>")
        sys.exit(1)

    start = int(sys.argv[1])
    amount = int(sys.argv[2])

    if not validate_parameters(start, amount):
        print("Invalid parameters. Start must be between 1 and 1000, and Amount must be
between 1 and 10.")
        sys.exit(1)

    sequence = fibonacci_sequence(start, amount)
    for num in sequence:
        print(num)


if __name__ == "__main__":
    main()
```

This script accepts two command-line parameters `start` and `amount`. It validates these
parameters to ensure they are within the specified ranges. If not valid, it prints an
error message and exits. Otherwise, it calculates the Fibonacci sequence starting at the
given `start` number for the given `amount` of numbers. Finally, it prints the calculated
sequence to the command line.

Please note that you can run this script from your terminal/command prompt by providing
the required parameters. For example: `python script.py 10 5`. This will generate a
Fibonacci sequence starting at 10 and consisting of 5 numbers.        


Step 7: Experiment!

Seems a little verbose (j/k), so lets ask for something smaller:

>>> Make the script as compact as possible but keep the verbosity of the command line parameter
... validation.
Here's a more compact version:

```
import sys

def main():
    try:
        starting_number, amount_of_numbers_produced = map(int, sys.argv[1:])

        if 0 < starting_number <= 1000 and 0 < amount_of_numbers_produced <= 10:
            sequence = [0, 1]
            for _ in range(amount_of_numbers_produced - 2):
                sequence.append((sequence[-1] + sequence[-2]) % 12345678)
            print(sequence[1:])
        else:
            print("Invalid input. Please enter a number less than or equal to 1000 for the
starting number and less than or equal to 10 for the count.")
    except (IndexError, ValueError):
        print("Invalid input. Please enter two command line parameters: the starting
number and the count.")

if __name__ == "__main__":
    main()
```
This version still checks the inputs in a clear and readable way, but it combines the
parameter extraction into one line and reduces the repeated validation conditions by using
Python's chained comparison feature (`0 < x <= y`).
        

And trying something different:

>>> I have a 50 character column restriction
Here is a revised version of the script that fits within a 50-character column
restriction:

```
import sys
def main(): try: start, count = map(int, sys.argv[1:]) if 1 <= start <= 1000 and 1 <=
count <= 10:
    print([0, 1]+[(a+b)%12345678 for a,b in zip([0]+[0]+[1], [0]+[1]+[0])][1:count])
except (IndexError, ValueError): print("Invalid input.")
if __name__ == "__main__": main()
```
Let me know if you have any further requests!
        


Hopefully you can see the value of running a LLM on your own laptop. Here are some other models you can try out:

Step 8: Try out Microsoft's phi3, a new compact LLM built on better curated inputs:

ollama run phi3
jpthomp137@silversurfer codetests % ollama run phi3
>>> create a sample normalized set of tables for system that tracks orders for an ecommerce w
... eb site, make DML compatible with MS SQL Server
Here's a sample normalized set of tables designed to track orders for an e-commerce
website. The following tables include primary keys (PK), foreign keys (FK) and are
compatible with Microsoft SQL Server:

1. Users Table:
```sql
CREATE TABLE Users (
    UserID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    Email VARCHAR(255) UNIQUE NOT NULL,
    PasswordHash VARBINARY(256) NOT NULL, -- Store hashed passwords here for security
    RegistrationDate DATETIME2 DEFAULT GETDATE()
);
```

2. Products Table:
```sql
CREATE TABLE Products (
    ProductID INT PRIMARY KEY,
    Name VARCHAR(100),
    Price DECIMAL(19, 4) NOT NULL, -- Store price with maximum precision
    StockQuantity INT NOT NULL,
    CategoryID INT,
    FOREIGN KEY (CategoryID) REFERENCES Categories(CategoryID)
);
```

3. Categories Table:
```sql
CREATE TABLE Categories (
    CategoryID INT PRIMARY KEY,
    Name VARCHAR(50),
    Description TEXT
);
```

4. OrderItems Table:
```sql
CREATE TABLE OrderItems (
    OrderItemID INT PRIMARY KEY,
    UserID INT FOREIGN KEY REFERENCES Users(UserID),
    ProductID INT FOREIGN KEY REFERENCES Products(ProductID),
    QuantityOrdered INT NOT NULL,
    TotalPrice DECIMAL(19, 4) AS (QuantityOrdered * Price) PERSISTED -- Calculate total
price on the fly as needed for queries.
);
```

5. Orders Table:
```sql
CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    UserID INT FOREIGN KEY REFERENCES Users(UserID),
    CreationDate DATETIME2 DEFAULT GETDATE(),
    ShippingAddressID INT, -- Assuming you have a separate Address table or shipping
details stored in this column.
    FOREIGN KEY (ShippingAddressID) REFERENCES ShippingAddresses(ShippingAddressID)
);
```

6. OrderDetails Table:
```sql
CREATE TABLE OrderDetails (
    OrderDetailID INT PRIMARY KEY,
    OrderID INT FOREIGN KEY REFERENCES Orders(OrderID),
    ProductID INT FOREIGN KEY REFERENCES Products(ProductID),
    QuantityInStockChanged INT DEFAULT 0 -- To keep track of changes to stock. You may
want to store the actual change instead, depending on requirements.
);
```

This schema provides a normalized foundation for tracking orders in an e-commerce
system using Microsoft SQL Server DML. Additional tables and relationships can be added
based on specific business needs, such as address information, shipping details, or
handling order statuses and payment information while ensuring data integrity through
appropriate foreign key constraints.
        

[Note of oops: Should've said DDL, and not DML; I guess LLMs are handy in sometimes guessing what we're really asking for.]


Oh, and try this:

>>> write SQL for finding the top 5 customers in total cost of orders that haven't purchased
... something in the past 6 months
To find the top 5 customers based on the total cost of their orders, excluding those
who have made purchases within the last 6 months, you can use a combination of
aggregate functions and subqueries. Here is an example SQL query:

```sql
WITH CustomerOrders AS (
    SELECT o.UserID, SUM(oi.TotalPrice) as TotalOrderCost
    FROM Orders o
    JOIN OrderItems oi ON o.OrderID = oi.OrderID
    WHERE NOT EXISTS (
        SELECT 1
        FROM Orders o2
        JOIN OrderItems oi2 ON o2.OrderID = oi2.OrderID
        WHERE o2.UserID = o.UserID AND o2.CreationDate > DATEADD(MONTH, -6, GETDATE())
    )
    GROUP BY o.UserID
)
SELECT TOP 5 c.FirstName, c.LastName, co.TotalOrderCost
FROM CustomerOrders co
JOIN Users c ON co.UserID = c.UserID
ORDER BY TotalOrderCost DESC;
```

This query first identifies the customers who have not placed any orders in the past 6
months using a subquery with the `NOT EXISTS` condition within the CTE (Common Table
Expression) named "CustomerOrders". It then calculates the total order cost for each
customer and retrieves their top 5 customers based on this aggregated data, excluding
those who have made recent purchases.        


In conclusion, hopefully you've seen a little bit of the power of LLMs and how to experiment on you own laptop. Some other models I recommend experimenting with are mistral and gemma.


What prompts or models have you found useful?

要查看或添加评论,请登录

社区洞察

其他会员也浏览了