Skip to content

Instantly share code, notes, and snippets.

View inchoate's full-sized avatar

Jason Vertrees inchoate

View GitHub Profile
@inchoate
inchoate / syncwrap.md
Created April 4, 2025 12:16
Syncwrap - small function to wrap your async code to run it safely in a sync context

syncwrap

syncwrap is a light and elegant solution for running asynchronous functions in a synchronous environment. This helper neatly packages your async tasks, handling event loop creation and cleanup so you don’t have to.

Code

import asyncio

def run_async_safely(async_func, *args, **kwargs):
@inchoate
inchoate / download_site.md
Last active October 10, 2024 13:13
Simple Site Downloader
#!/bin/zsh

# Usage:
#     ./download_site.sh {URL}


url=$1
output_dir="./sites"
@inchoate
inchoate / moving-to-fastapi-async.md
Created September 24, 2024 20:15
Moving to FastAPI Async

Moving from Sync to Async in FastAPI with SQLModel? Here's What You Need to Know!

Switching from a synchronous setup to an asynchronous one in your FastAPI/SQLModel app can bring some challenges. When making this switch, several important details need attention, especially around async sessions, avoiding misuse of await, and correctly handling relationships between tables.

A Typical Synchronous Setup:

# sync version
engine = create_engine(database_settings.pg_connection_string)
session = sessionmaker(bind=engine)
@inchoate
inchoate / running-out-of-db-connections-in-fastapi.md
Created September 2, 2024 15:57
How to Fix Running out of DB Connections in FastAPI

Ran Out of Database Connections on Your SQLModel App? Here’s a Quick Fix!

Problem:

You might have run into a situation where your SQLModel app unexpectedly runs out of database connections. If that sounds familiar, you probably have something like this in your code:

# utils/db.py
def get_db():
 """A utility function to grab a database session."""
@inchoate
inchoate / enums_in_sqlmodel_with_postgres.md
Created August 22, 2024 19:34
Using Enums in SQLModel with Postgres

Working with Enums in SQLModel and PostgreSQL: A Tasty Example with Ice Cream Flavors

Introduction:
Enums are a great way to represent a fixed set of values in your database. In this example, we'll model an IceCreamShop that serves various flavors of ice cream using SQLModel and PostgreSQL. We'll demonstrate how to store these flavors using enums and how to query for specific flavors using SQLAlchemy's powerful querying capabilities.

import os
from enum import Enum
from typing import List, Optional
@inchoate
inchoate / rename_field_in_json_postgres.sql
Created August 20, 2024 14:17
Example showing how to rename a field contained within a JSON column in postgres.
--
-- This nice little query renames `camp_id` to `organization_id` in the JSON column.
-- Note, it converts to and from JSONB to do this.
--
UPDATE public.data_camp_embeddings
SET metadata_ = jsonb_set(
-- Convert JSON to JSONB for easier manipulation, and remove the "camp_id" key
metadata_::jsonb - 'camp_id',
-- Insert "organization_id" key at the top level with the value from "camp_id"
'{organization_id}',
@inchoate
inchoate / quick-vector-db-for-ai.md
Created August 10, 2024 12:07
Vector database for AI

Quick Setup: PostgreSQL with pgvector for AI Projects

If you need a working vector database for your AI project in no time, this guide is for you. Setting up PostgreSQL with pgvector is super easy, and Docker makes it even more convenient.

Step 1: Run PostgreSQL with pgvector

PostgreSQL is an excellent database, and Docker images make it easy to spin up a containerized version. To get PostgreSQL with pgvector installed and ready to go, run the following command:

docker run -d --rm \
@inchoate
inchoate / geolocation_mixin.md
Created August 9, 2024 10:26
geolocation_mixin.py

Geolocation with SQLModel

Need to use postgis with SQLModel? This example will show you how.

This gist demonstrates how to integrate a geolocation field into your SQLModel projects by directly adding a geometry column representing a point on Earth. The GeolocationMixin class provides an easy way to store and serialize geospatial data, specifically using PostGIS to handle POINT geometry types.

To run this example, you'll need a PostGIS-enabled database. You can set up PostGIS using Docker with the following command:

docker run -d --rm \
@inchoate
inchoate / timestamp_mixin.py
Last active August 8, 2024 11:08
SQLModel Timestamp Mixing - add the usual fields to your data model with ease
"""
Adding this SQLModel mixin will add created_at, updated_at, and deleted_at to your tables.
`created_at` will auto-populate upon creation. `updated_at` will do the right thing on updates.
Usage:
class YourModel(TimestampMixin):
...
At this point all classes inheriting from YourModel will have the usual fields added.
@inchoate
inchoate / filtered-query-engine.md
Last active August 10, 2024 12:32
Llama Index Filtered Query Engine Example

Selectively Querying Documents in Llama Index

Note: This gist has been updated to be far simpler than the original implementation, focusing on a more streamlined approach to selectively querying documents based on metadata.

Introduction

When working with Llama Index and other Retrieval-Augmented Generation (RAG) systems, most tutorials focus on ingesting and querying a single document. You typically read the document from a source, parse it, embed it, and store it in your vector store. Once there, querying is straightforward. But what if you have multiple documents and want to selectively query only one, such as Document #2 (doc_id=2), from your vector store?

This article demonstrates how to encapsulate the creation of a filtered query engine, which allows you to specify the nodes to query based on custom metadata. This approach provides a more structured and efficient way to retrieve relevant information, making it easier to manage and scale your querying process.