Skip to content

Commit

Permalink
docs: Add documentation for GroqChatTarget (#704)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsdlm committed Feb 13, 2025
1 parent 7e3463d commit a04d162
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 0 deletions.
121 changes: 121 additions & 0 deletions doc/code/targets/groq_target.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "3ca236f0",
"metadata": {
"lines_to_next_cell": 0
},
"source": [
"# GroqChatTarget\n",
"\n",
"This example demonstrates how to use the `GroqChatTarget` class in PyRIT to send a prompt \n",
"to a Groq model and retrieve a response.\n",
"\n",
"## Setup\n",
"Before running this example, you need to set the following environment variables:\n",
"\n",
"```\n",
"export GROQ_API_KEY=\"your_api_key_here\"\n",
"export GROQ_MODEL_NAME=\"llama3-8b-8192\"\n",
"```\n",
"\n",
"Alternatively, you can pass these values as arguments when initializing `GroqChatTarget`:\n",
"\n",
"```python\n",
"groq_target = GroqChatTarget(model_name=\"llama3-8b-8192\", api_key=\"your_api_key_here\")\n",
"```\n",
"\n",
"You can also limit the request rate using `max_requests_per_minute`.\n",
"\n",
"## Example\n",
"The following code initializes `GroqChatTarget`, sends a prompt using `PromptSendingOrchestrator`, \n",
"and retrieves a response."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "bb1f3d7f",
"metadata": {
"execution": {
"iopub.execute_input": "2025-02-13T13:47:56.016223Z",
"iopub.status.busy": "2025-02-13T13:47:56.016098Z",
"iopub.status.idle": "2025-02-13T13:48:00.901464Z",
"shell.execute_reply": "2025-02-13T13:48:00.900848Z"
}
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"None of PyTorch, TensorFlow >= 2.0, or Flax have been found. Models won't be available and only tokenizers, configuration and file/data utilities can be used.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"\u001b[22m\u001b[39mConversation ID: 6dd12f79-c5f1-49ea-b0fd-2e85b71a5066\n",
"\u001b[1m\u001b[34muser: Why is the sky blue ?\n",
"\u001b[22m\u001b[33massistant: A classic question!\n",
"\n",
"The sky appears blue because of a phenomenon called Rayleigh scattering, which is the scattering of light by small particles or molecules in the atmosphere. Here's a simplified explanation:\n",
"\n",
"1. **Sunlight enters the Earth's atmosphere**: When sunlight enters our atmosphere, it contains all the colors of the visible spectrum, which we know as white light.\n",
"2. **Short wavelengths scatter more**: As sunlight travels through the atmosphere, it encounters tiny molecules of gases like nitrogen (N2) and oxygen (O2). These molecules are much smaller than the wavelength of light, so they scatter the shorter wavelengths of light more efficiently than the longer wavelengths.\n",
"3. **Blue light is scattered more**: The shorter wavelengths of light, like blue and violet, are scattered more than the longer wavelengths, like red and orange. This is because the smaller molecules are more effective at scattering shorter wavelengths.\n",
"4. **Scattered light reaches our eyes**: As the scattered blue light travels through the atmosphere, it reaches our eyes from all directions. Since the blue light is scattered in all directions, it appears to come from all parts of the sky.\n",
"5. **Red light continues to travel in a straight line**: Meanwhile, the longer wavelengths of light, like red and orange, are not scattered as much. They continue to travel in a straight line, reaching our eyes only from the direction of the sun.\n",
"6. **Our eyes perceive the blue color**: Due to the dominance of scattered blue light, our eyes perceive the sky as blue. The exact shade of blue can vary depending on Atmospheric conditions, such as pollution, dust, and water vapor, which can scatter light in different ways.\n",
"\n",
"In summary, the sky appears blue because of the scattering of sunlight by small molecules in the atmosphere, which favors shorter wavelengths like blue and violet over longer wavelengths like red and orange.\n"
]
}
],
"source": [
"\n",
"import asyncio\n",
"from pyrit.common import IN_MEMORY, initialize_pyrit\n",
"from pyrit.orchestrator import PromptSendingOrchestrator\n",
"from pyrit.prompt_target import GroqChatTarget\n",
"\n",
"initialize_pyrit(memory_db_type=IN_MEMORY)\n",
"\n",
"groq_target = GroqChatTarget()\n",
"\n",
"prompt = \"Why is the sky blue ?\"\n",
"\n",
"orchestrator = PromptSendingOrchestrator(objective_target=groq_target)\n",
"\n",
"response = await orchestrator.send_prompts_async(prompt_list=[prompt])\n",
"await orchestrator.print_conversations_async()"
]
}
],
"metadata": {
"jupytext": {
"cell_metadata_filter": "-all"
},
"kernelspec": {
"display_name": "pyrt_env",
"language": "python",
"name": "pyrt_env"
},
"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.11.2"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
57 changes: 57 additions & 0 deletions doc/code/targets/groq_target.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# ---
# jupyter:
# jupytext:
# cell_metadata_filter: -all
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.16.7
# kernelspec:
# display_name: pyrt_env
# language: python
# name: pyrt_env
# ---

# %% [markdown]
# # GroqChatTarget
#
# This example demonstrates how to use the `GroqChatTarget` class in PyRIT to send a prompt
# to a Groq model and retrieve a response.
#
# ## Setup
# Before running this example, you need to set the following environment variables:
#
# ```
# export GROQ_API_KEY="your_api_key_here"
# export GROQ_MODEL_NAME="llama3-8b-8192"
# ```
#
# Alternatively, you can pass these values as arguments when initializing `GroqChatTarget`:
#
# ```python
# groq_target = GroqChatTarget(model_name="llama3-8b-8192", api_key="your_api_key_here")
# ```
#
# You can also limit the request rate using `max_requests_per_minute`.
#
# ## Example
# The following code initializes `GroqChatTarget`, sends a prompt using `PromptSendingOrchestrator`,
# and retrieves a response.
# %%

import asyncio
from pyrit.common import IN_MEMORY, initialize_pyrit
from pyrit.orchestrator import PromptSendingOrchestrator
from pyrit.prompt_target import GroqChatTarget

initialize_pyrit(memory_db_type=IN_MEMORY)

groq_target = GroqChatTarget()

prompt = "Why is the sky blue ?"

orchestrator = PromptSendingOrchestrator(objective_target=groq_target)

response = await orchestrator.send_prompts_async(prompt_list=[prompt])
await orchestrator.print_conversations_async()

0 comments on commit a04d162

Please sign in to comment.