Skip to content

Instantly share code, notes, and snippets.

@iamaziz
Last active July 30, 2024 15:01
Show Gist options
  • Save iamaziz/857306469505748bf79f49a2a9a7a722 to your computer and use it in GitHub Desktop.
Save iamaziz/857306469505748bf79f49a2a9a7a722 to your computer and use it in GitHub Desktop.
Getting started with DSPy and Ollama
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Minimal DSPy example with Ollama LLM"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import dspy\n",
"\n",
"dspy.settings.configure(lm=dspy.OllamaLocal(model='mistral-nemo'))\n",
"\n",
"class BasicQA(dspy.Signature):\n",
" \"\"\"Answer questions with short factoid answers.\"\"\"\n",
" question = dspy.InputField()\n",
" answer = dspy.OutputField(desc=\"a markdown formatted table containing two columns: question and answer\")"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"| Question | Answer |\n",
"| --- | --- |\n",
"| What is the secret of life? | \"42\", according to The Hitchhiker's Guide to the Galaxy by Douglas Adams. |\n",
"\n",
"---\n"
]
}
],
"source": [
"# -- Example 1 -- a minimal example\n",
"generate_answer = dspy.Predict(BasicQA)\n",
"answer = generate_answer(question=\"What is the secret of life?\").answer\n",
"print(answer)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Prediction(\n",
" rationale='**Answer:**\\n\\n| Question | Answer |\\n| --- | --- |\\n| What is the secret of life? | \"42\", according to The Hitchhiker\\'s Guide to the Galaxy. |\\n\\n**Explanation:** This answer is a reference to the book and film series, where the supercomputer Deep Thought gives the ultimate answer to life, the universe, and everything as 42.',\n",
" answer='| Question | Answer |\\n| --- | --- |\\n| What is the secret of life? | \"42\" |'\n",
")\n"
]
}
],
"source": [
"# -- Example 2 -- a chain of thought object\n",
"chain_of_thought = dspy.ChainOfThought(BasicQA)\n",
"cot_answer = chain_of_thought(question=\"What is the secret of life?\")\n",
"print(cot_answer)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"rationale **Answer:**\n",
"\n",
"| Question | Answer |\n",
"| --- | --- |\n",
"| What is the secret of life? | \"42\", according to The Hitchhiker's Guide to the Galaxy. |\n",
"\n",
"**Explanation:** This answer is a reference to the book and film series, where the supercomputer Deep Thought gives the ultimate answer to life, the universe, and everything as 42.\n",
"answer | Question | Answer |\n",
"| --- | --- |\n",
"| What is the secret of life? | \"42\" |\n"
]
}
],
"source": [
"for k in cot_answer.keys(): print(k, cot_answer[k])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"\n",
"## Pretty print the answers"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"| Question | Answer |\n",
"| --- | --- |\n",
"| What is the secret of life? | \"42\", according to The Hitchhiker's Guide to the Galaxy by Douglas Adams. |\n",
"\n",
"---"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"from IPython.display import Markdown, display\n",
"display(Markdown(answer))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/markdown": [
"### rationale"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"**Answer:**\n",
"\n",
"| Question | Answer |\n",
"| --- | --- |\n",
"| What is the secret of life? | \"42\", according to The Hitchhiker's Guide to the Galaxy. |\n",
"\n",
"**Explanation:** This answer is a reference to the book and film series, where the supercomputer Deep Thought gives the ultimate answer to life, the universe, and everything as 42."
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"### answer"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/markdown": [
"| Question | Answer |\n",
"| --- | --- |\n",
"| What is the secret of life? | \"42\" |"
],
"text/plain": [
"<IPython.core.display.Markdown object>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"for k in cot_answer.keys(): \n",
" display(Markdown(f\"### {k}\"))\n",
" display(Markdown(cot_answer[k]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"\n",
"> https://github.com/stanfordnlp/dspy"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment