Skip to content

Commit

Permalink
Add answer generation example notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
janheinrichmerker committed Jun 3, 2024
1 parent a198d6f commit aaaf234
Showing 1 changed file with 229 additions and 0 deletions.
229 changes: 229 additions & 0 deletions notebooks/generation.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load the API keys from the `.env` file in the root directory."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"from dotenv import load_dotenv, find_dotenv\n",
"\n",
"if find_dotenv():\n",
" load_dotenv()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Configure the LLM model."
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"LLM_NAME = \"Mistral-7B-Instruct-v0.2\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Blablador](https://helmholtz-blablador.fz-juelich.de/) uses different names, so here, we create a quick mapping of the supported models."
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [],
"source": [
"BLABLADOR_MODEL_NAMES = {\n",
" \"Mistral-7B-Instruct-v0.2\": \"1 - Mistral-7B-Instruct-v0.2 - the best option in general - fast and good\",\n",
" \"Mixtral-8x7B-Instruct-v0.1\": \"2 - Mixtral-8x7B-Instruct-v0.1 Slower with higher quality\",\n",
" \"starcoder2-15b\": \"3 - starcoder2-15b - A model for programming\",\n",
" \"GritLM-7B\": \"5 - GritLM-7B - For Text-Embeddings\",\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"from os import environ\n",
"from dspy import OpenAI, settings as dspy_settings\n",
"\n",
"llm = OpenAI(\n",
" model=(\n",
" BLABLADOR_MODEL_NAMES[LLM_NAME]\n",
" if LLM_NAME in BLABLADOR_MODEL_NAMES.keys()\n",
" else LLM_NAME),\n",
" api_key=environ[\"OPENAI_API_KEY\"],\n",
" api_base=environ.get(\"OPENAI_API_BASE\"),\n",
" temperature=0,\n",
" max_tokens=250,\n",
" top_p=1,\n",
" frequency_penalty=0,\n",
" presence_penalty=0,\n",
" n=1,\n",
")\n",
"dspy_settings.configure(lm=llm)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Test the LLM with a basic prompt."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'id': 'cmpl-3XM99r5xwT3n9niRgEKdvj',\n",
" 'choices': [{'finish_reason': 'stop',\n",
" 'index': 0,\n",
" 'logprobs': None,\n",
" 'text': ' over a lazy dog.\\n\\nThis sentence is known as a pangram, a sentence that uses every letter of the alphabet at least once. It\\'s a fun little language trick that can help you practice your typing skills or just impress your friends.\\n\\nBut where did this particular pangram come from? The origin of the sentence is a bit murky, but it\\'s believed to have first appeared in print in the early 20th century. One popular theory is that it was created by a typesetter named Peter Mark Roget, who is also known for compiling the famous thesaurus.\\n\\nAccording to this theory, Roget was working on a typesetting manual and wanted to include a pangram as an example. He came up with the sentence \"A quick brown fox jumps over a lazy dog\" because it used every letter of the alphabet and flowed well when typed.\\n\\nAnother theory is that the sentence was inspired by a line from Edward Lear\\'s nonsense poem \"The Jumblies,\" which reads \"They went to sea in a Sieve, they did.\" This sentence also uses every letter of the alphabet, but it'}],\n",
" 'created': 1717408054,\n",
" 'model': '1 - Mistral-7B-Instruct-v0.2 - the best option in general - fast and good',\n",
" 'object': 'text_completion',\n",
" 'system_fingerprint': None,\n",
" 'usage': {'completion_tokens': 250, 'prompt_tokens': 8, 'total_tokens': 258}}"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"llm.basic_request(prompt=\"A quick brown fox jumps\")"
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"from dspy import Module, Predict\n",
"from dspy.primitives.prediction import Prediction\n",
"\n",
"@dataclass(frozen=True)\n",
"class AnswerPredict(Module):\n",
" predict: Predict = Predict(signature=\"question -> answer\")\n",
" \n",
" def forward(self, question: str) -> Prediction:\n",
" return self.predict(question=question)"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Prediction(\n",
" answer='Ibuprofen is a nonsteroidal anti-inflammatory drug (NSAID) that works by reducing the production of prostaglandins, which are hormone-like substances that cause pain and inflammation in the body. In the case of a headache, Ibuprofen helps to relieve the pain by reducing inflammation in the blood vessels surrounding the brain. Additionally, Ibuprofen also acts as a analgesic, which means it directly blocks the transmission of pain signals to the brain. By reducing inflammation and blocking pain signals, Ibuprofen effectively treats headaches and provides relief.'\n",
")"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"answer_predict = AnswerPredict()\n",
"answer_predict(question=\"How does Ibuprofen work to treat headaches?\")"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [],
"source": [
"from dataclasses import dataclass\n",
"from dspy import Module, ChainOfThought\n",
"from dspy.primitives.prediction import Prediction\n",
"\n",
"\n",
"@dataclass(frozen=True)\n",
"class AnswerChainOfThought(Module):\n",
" chain_of_thought: ChainOfThought = ChainOfThought(\n",
" signature=\"question -> answer\"\n",
" )\n",
"\n",
" def forward(self, question: str) -> Prediction:\n",
" return self.chain_of_thought(question=question)"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"Prediction(\n",
" rationale='understand how Ibuprofen works to treat headaches. Ibuprofen is a non-steroidal anti-inflammatory drug (NSAID). It works by inhibiting the production of prostaglandins, which are hormone-like substances that cause pain and inflammation. In the case of a headache, the blood vessels in the head dilate, causing pain. Ibuprofen reduces the production of prostaglandins, which in turn reduces inflammation and helps to narrow the dilated blood vessels, thus relieving the headache.',\n",
" answer='Ibuprofen works by inhibiting the production of prostaglandins, which are hormone-like substances that cause pain and inflammation. By doing so, it reduces inflammation and helps to narrow dilated blood vessels, thus relieving headaches.'\n",
")"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"answer_chain_of_thought = AnswerChainOfThought()\n",
"answer_chain_of_thought(question=\"How does Ibuprofen work to treat headaches?\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"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.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

0 comments on commit aaaf234

Please sign in to comment.