Skip to content

Instantly share code, notes, and snippets.

@cboettig
cboettig / llm-structured-response.py
Created November 23, 2024 05:04
langchain structured sql reply
import streamlit as st
import os
#os.environ["OPENAI_API_KEY"] = st.secrets['OPENAI_API_KEY'] # for gpt
os.environ["OPENAI_API_KEY"] = st.secrets['LITELLM_KEY'] # for litellm
from pydantic import BaseModel, Field
class SQLResponse(BaseModel):
"""Defines the structure for SQL response."""
@cboettig
cboettig / ibis-langchain-duckdb.py
Last active November 21, 2024 22:42
langchain + ibis share a connection
parquet = "https://espm-157-f24.github.io/spatial-carl-amanda-tyler/new_haven_stats.parquet"
# create sharable low-level connection, see: https://github.com/Mause/duckdb_engine
import sqlalchemy
eng = sqlalchemy.create_engine("duckdb:///:memory:")
# ibis can talk to this connection and create the VIEW
import ibis
con = ibis.duckdb.from_connection(eng.raw_connection())
@cboettig
cboettig / duckdb-s3.py
Created November 9, 2024 00:45
NRP use of S3 + duckdb
%%time
import ibis
from ibis import _
con = ibis.duckdb.connect()
endpoint="rook-ceph-rgw-nautiluss3.rook"
query= f'''
CREATE OR REPLACE SECRET secret2 (
@cboettig
cboettig / ibis_to_json.py
Created November 7, 2024 20:20
ibis / raw sql workarounds
import ibis
from ibis import _
con = ibis.duckdb.connect()
# example query, could be anything
subset = (gbif
.filter(_["class"] == "Aves")
.rename(hex = "h8")
.group_by(_.hex)
.agg(n = _.count())
@cboettig
cboettig / geoarrow-error.md
Created November 4, 2024 16:36
ibis geo error

To solve

ImportError: cannot import name 'types' from 'geoarrow' (unknown location), 

install geoarrow-types.

ibis may also need geoarrow-cto work with geoparquet now.

import os
from langchain_openai import ChatOpenAI
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import ConfigurableField
from langchain_core.tools import tool
from langchain.agents import create_tool_calling_agent, AgentExecutor
@tool
def multiply(x: float, y: float) -> float:
"""Multiply 'x' times 'y'."""
return x * y
@cboettig
cboettig / python-access.py
Created October 24, 2024 16:56
forecast access
import ibis
con = ibis.duckdb.connect()
con.raw_sql(f'''
CREATE OR REPLACE SECRET secret (
TYPE S3,
ENDPOINT 'sdsc.osn.xsede.org',
URL_STYLE 'path'
);
@cboettig
cboettig / ibis_geo.py
Last active August 23, 2024 18:16
ibis geospatial I/O
import ibis
from ibis import _
con = ibis.duckdb.connect(extensions=["spatial"])
## Read a geospatial file other than parquet (via GDAL)
rivers_geojson = "https://data.source.coop/cboettig/us-rivers/nhd_flowline_national.geojson"
rivers = con.read_geo(rivers_geojson)
## Read geoparquet
@cboettig
cboettig / debug.py
Last active July 31, 2024 16:43
fishbase export in python
conn.sql('''
SELECT *
FROM information_schema.columns
WHERE table_name = 'families';
'''
)
@cboettig
cboettig / gurobi-setup.sh
Created April 1, 2024 02:42
gurobi-setup.sh
# Be sure to put a copy of the license file in "$HOME/gurobi.lic" for this to work!!
# Download the release
wget https://packages.gurobi.com/11.0/gurobi11.0.1_linux64.tar.gz # or path to most recent linux_64 download from https://www.gurobi.com/downloads/gurobi-software/
# extract:
tar -xvf gurobi*_linux64.tar.gz
# Set environmental variables