forked from groq/groq-api-cookbook
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3941d39
commit 46f84bf
Showing
3 changed files
with
310 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
304 changes: 304 additions & 0 deletions
304
tutorials/jigsawstack-prompt-engine/jigsawstack-prompt-engine.ipynb
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,304 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# JigsawStack Prompt Engine" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"[JigsawStack](https://jigsawstack.com) is an AI SDK that is easy to plug and play into any backend to automate a lot of the heavy lifting away for tasks like scraping, OCR, translation, and using LLMs.\n", | ||
"\n", | ||
"The JigsawStack Prompt Engine allows you to run the best LLM on every prompt at the fastest speed powered by Groq!" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Features" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"🌐**Prompt caching**: Stores prompts and responses for quick reuse, significantly reducing latency for repeated queries.\n", | ||
"\n", | ||
"💬**Automatic prompt optimization**: Automatically refines prompts to enhance response accuracy and efficiency from the language model.\n", | ||
"\n", | ||
"📄**Response schema validation**: Ensures that responses adhere to a predefined format or structure, providing consistency in output.\n", | ||
"\n", | ||
"🔁**Reusable prompts**: Allows prompts to be reused across different sessions and workflows, saving time in repetitive tasks.\n", | ||
"\n", | ||
"🧠**Multi-agent LLM from 50+ models**: Automatically selects the best model suitable for the task from the poll of models.\n", | ||
"\n", | ||
"🚫 **No virtual rate limits, tokens, and GPU management**\n" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Prerequisite " | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"- Create a JigsawStack [account](https://jigsawstack.com) (Get started for free)\n", | ||
"- Retrieve your api [key](https://jigsawstack.com/dashboard)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Setup" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Install the JigsawStack SDK" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"metadata": { | ||
"trusted": true | ||
}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"Requirement already satisfied: jigsawstack in /opt/anaconda3/envs/openvoice/lib/python3.9/site-packages (0.1.14)\n", | ||
"Requirement already satisfied: requests>=2.31.0 in /opt/anaconda3/envs/openvoice/lib/python3.9/site-packages (from jigsawstack) (2.32.2)\n", | ||
"Requirement already satisfied: typing-extensions in /opt/anaconda3/envs/openvoice/lib/python3.9/site-packages (from jigsawstack) (4.11.0)\n", | ||
"Requirement already satisfied: charset-normalizer<4,>=2 in /opt/anaconda3/envs/openvoice/lib/python3.9/site-packages (from requests>=2.31.0->jigsawstack) (2.0.4)\n", | ||
"Requirement already satisfied: idna<4,>=2.5 in /opt/anaconda3/envs/openvoice/lib/python3.9/site-packages (from requests>=2.31.0->jigsawstack) (3.7)\n", | ||
"Requirement already satisfied: urllib3<3,>=1.21.1 in /opt/anaconda3/envs/openvoice/lib/python3.9/site-packages (from requests>=2.31.0->jigsawstack) (2.2.2)\n", | ||
"Requirement already satisfied: certifi>=2017.4.17 in /opt/anaconda3/envs/openvoice/lib/python3.9/site-packages (from requests>=2.31.0->jigsawstack) (2024.7.4)\n", | ||
"Note: you may need to restart the kernel to use updated packages.\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"%pip install jigsawstack" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"Import and Initialize the SDK" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"trusted": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"from jigsawstack import JigsawStack\n", | ||
"\n", | ||
"jigsaw = JigsawStack(api_key=\"your-api-key\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Usage" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Example 1: Create and run a prompt" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"- #### Create prompt" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"trusted": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"params = {\n", | ||
" \"prompt\": \"How to cook {dish}\", #The prompt for your use case\n", | ||
" \"inputs\": [{ \"key\": \"dish\" }], #dynamic vars that are in the brackets {}\n", | ||
" \"return_prompt\": [{\n", | ||
" \"step\": \"name of this step\",\n", | ||
" \"details\": \"details of this step\",\n", | ||
" }], #The structure of the JSON, in this case, an array of objects\n", | ||
"}\n", | ||
"result = jigsaw.prompt_engine.create(params)\n", | ||
"\n", | ||
"print(result.prompt_engine_id) # prompt engine ID" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"- #### Run the prompt" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"trusted": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"resp = jigsaw.prompt_engine.run(\n", | ||
" {\n", | ||
" \"id\": result.prompt_engine_id, #The ID you got after creating the engine\n", | ||
" \"input_values\": {\n", | ||
" \"dish\": \"Singaporean chicken rice\", #They value for your dynamic field\n", | ||
" },\n", | ||
" }\n", | ||
")\n", | ||
"\n", | ||
"print(resp)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"### Example 2: Execute the prompt directly" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"trusted": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"params = {\n", | ||
" \"prompt\":\"How to cook {dish}\",\n", | ||
" \"inputs\": [\n", | ||
" {\n", | ||
" \"key\": \"dish\"\n", | ||
" },\n", | ||
" ],\n", | ||
" \"input_values\": {\n", | ||
" \"dish\": \"Nigerian Jollof Rice\"\n", | ||
" },\n", | ||
" \"return_prompt\": [{\n", | ||
" \"step\": \"name of this step\",\n", | ||
" \"details\": \"details of this step\",\n", | ||
" }],\n", | ||
"}\n", | ||
"\n", | ||
"result = jigsaw.prompt_engine.run_prompt_direct(params)\n", | ||
"\n", | ||
"print(result)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Prompt Guard - Llama Guard 3 by Groq" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"The prompt engine comes with prompt guards to prevent prompt injection from user inputs and a wide range of unsafe use cases. This can be turned on automatically using the `prompt_guard` field." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"metadata": { | ||
"trusted": true | ||
}, | ||
"outputs": [], | ||
"source": [ | ||
"params = {\n", | ||
" \"prompt\": \"Tell me a story about {about}\",\n", | ||
" \"inputs\": [\n", | ||
" {\n", | ||
" \"key\": \"about\",\n", | ||
" },\n", | ||
" ],\n", | ||
" \"input_values\": {\n", | ||
" \"about\": \"The Leaning Tower of Pisa\"\n", | ||
" },\n", | ||
" \"return_prompt\": \"Return the result in a markdown format\",\n", | ||
" \"prompt_guard\": [\"sexual_content\", \"defamation\"] #Add this to use llama-guard\n", | ||
"}\n", | ||
"result = jigsaw.prompt_engine.run_prompt_direct(params)\n", | ||
"\n", | ||
"print(result)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"## Recommendation" | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"\n", | ||
"- For prompts that are used repeatedly, it is recommended to first create the prompt, then run it using its prompt ID to enable optimization.\n", | ||
"\n", | ||
"- Run Prompt Direct is ideal for one-time use.\n", | ||
"\n", | ||
"For more information on the Prompt Engine engine see [documentation](https://docs.jigsawstack.com/examples/ai/prompt-engine)" | ||
] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python env", | ||
"language": "python", | ||
"name": "openvoice" | ||
}, | ||
"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.19" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 4 | ||
} |
This file was deleted.
Oops, something went wrong.