Last active
August 18, 2025 21:26
-
-
Save dbreunig/e8e64035d7c7c5812e54120f351af1a0 to your computer and use it in GitHub Desktop.
Quickly building a tool-using DSPy module that leverages Chroma Cloud search.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import chromadb | |
import dspy | |
# Chose your LM. I'm using Qwen3-4B, hosted by LM Studio | |
lm = dspy.LM( | |
'openai/qwen_qwen3-4b-thinking-2507', | |
api_base="http://127.0.0.1:1234/v1", | |
api_key="blah", | |
max_tokens=262144 | |
) | |
dspy.configure(lm=lm) | |
# Connect to Chroma | |
client = chromadb.CloudClient( | |
api_key='YOUR_KEY', | |
tenant='YOUR_TENANT', | |
database='YOUR_DB' | |
) | |
# A function for DSPy to use that preps the result for LM usage | |
def find_relevant_content(query): | |
""" | |
Find relevant snippets from blog posts that are similar to the provided query. | |
""" | |
collection = client.get_or_create_collection(name='posts') | |
results = collection.query(query_texts=[query], include=['documents']) | |
return results['documents'][0] | |
# The DSPy signature | |
class ParagraphWriter(dspy.Signature): | |
""" | |
Given an intro sentence, generate a complete paragraph. | |
Only use information you obtain from any tools, though you can rewrite what you find. | |
""" | |
intro_sentence: str = dspy.InputField() | |
completed_paragraph: str = dspy.OutputField() | |
# The DSPy module | |
writer = dspy.ReAct(ParagraphWriter, tools=[find_informing_content]) | |
# Try it out! | |
result = writer(intro_sentence="AI is reshaping how we labor, learn, and communicate, yet most of us don’t understand how these systems work.") | |
print(result.completed_paragraph) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment