Skip to content

Instantly share code, notes, and snippets.

@sb-static
sb-static / sgr_assistant.py
Last active September 3, 2025 01:55
SGR Demo - assistant based on small LLM (Qwen3-4B) using llama.cpp and Schema-Guided Reasoning (SGR)
"""
This Python code demonstrates Schema-Guided Reasoning (SGR) with llama.cpp and small model - Qwen3-4B Q8_0. It:
- implements a business agent capable of planning and reasoning
- implements tool calling using only SGR and simple dispatch
- uses with a simple (inexpensive) non-reasoning model for that
This demo is modified from https://abdullin.com/schema-guided-reasoning/demo to support local llm
Test model: Qwen3-4B-Instruct-2507-Q8_0 (https://huggingface.co/unsloth/Qwen3-4B-Instruct-2507-GGUF/resolve/main/Qwen3-4B-Instruct-2507-Q8_0.gguf)
@abdullin
abdullin / schema-guided-reasoning.py
Last active September 10, 2025 13:13
Demo for a Schema-Guided Reasoning (Business Assistant)
"""
This Python code demonstrates Schema-Guided Reasoning (SGR) with OpenAI. It:
- implements a business agent capable of planning and reasoning
- implements tool calling using only SGR and simple dispatch
- uses with a simple (inexpensive) non-reasoning model for that
To give this agent something to work with, we ask it to help with running
a small business - selling courses to help to achieve AGI faster.
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain_community.document_loaders import WebBaseLoader
from langchain_community.vectorstores import Chroma
from langchain_community.embeddings.sentence_transformer import SentenceTransformerEmbeddings
from typing import Annotated, Dict, TypedDict
from langchain_core.messages import BaseMessage
import json
import operator
from typing import Annotated, Sequence, TypedDict
def expected_steps(df):
Q = df.drop(
['Null', 'Activation'], axis=1).drop(['Null', 'Activation'], axis=0)
I = np.identity(Q.shape[1])
N = np.linalg.inv(I - Q.to_numpy())
t = np.sum(N, axis=1)
plt.figure(figsize=(10,5))
sns.scatterplot(data=df_scatter, x='Click Activation Rate', y='Activation Rate', s=200, color='#2653de')
for line in range(0, df_scatter.shape[0]):
plt.text(df_scatter['Click Activation Rate'][line]+0.001, df_scatter['Activation Rate'][line],
df_scatter['Channel'][line], horizontalalignment='left',
size='medium', color='black', weight='semibold')
df_scatter = df_multi.copy()
df_scatter['Coverage'] = df_scatter['Channel'].map(
campaign_data.groupby('channel')['customer_id'].nunique().to_dict()
)
df_scatter['Total Clicks'] = df_scatter['Channel'].map(
journeys['path'].apply(lambda x: x[-2]).value_counts().to_dict()
)
df_multi = pd.DataFrame({
'Channel': attributions.keys(),
'Attribution style': 'Journey',
'Activations': attributions.values()
})
df_first = pd.DataFrame({
'Channel': attributions.keys(),
'Attribution style': 'First touchpoint'
})
def removal_effects(df, conversion_rate):
removal_effects_dict = {}
channels = [channel for channel in df.columns if channel not in ['Start',
'Null',
'Activation']]
for channel in channels:
removal_df = df.drop(channel, axis=1).drop(channel, axis=0)
for column in removal_df.columns:
row_sum = np.sum(list(removal_df.loc[column]))
null_pct = float(1) - row_sum
import matplotlib.pyplot as plt
import seaborn as sns
from markovchain import MarkovChain
mc = MarkovChain(trans_matrix.values, trans_matrix.index)
mc.draw()
# Function to create intermediate path strings
def transition_states(paths):
unique_channels = set(x for element in paths for x in element)
transition_states = {x + '>' + y: 0 for x in unique_channels for y in unique_channels}
for possible_state in unique_channels:
if possible_state not in ['Activation', 'Null']:
for user_path in paths:
if possible_state in user_path:
indices = [i for i, s in enumerate(user_path) if possible_state in s]