Skip to content

Commit

Permalink
update for 3.1 native tool use support
Browse files Browse the repository at this point in the history
  • Loading branch information
hozen-groq committed Sep 7, 2024
1 parent d57c6a7 commit 7caf0fa
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 3 deletions.
96 changes: 96 additions & 0 deletions tutorials/llamaguard/ai_guard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import gradio as gr
import base64
from PIL import Image
import io
import requests
from groq import Groq

# Groq API settings
LLAVA_MODEL = "llava-v1.5-7b-4096-preview"
LLAMA_GUARD_MODEL = "llama-guard-3-8b"

def encode_image(image):
buffered = io.BytesIO()
image.save(buffered, format="JPEG")
return base64.b64encode(buffered.getvalue()).decode('utf-8')

def analyze_image(image, prompt, api_key, is_url=False):
client = Groq(api_key=api_key)

if is_url:
image_content = {"type": "image_url", "image_url": {"url": image}}
else:
base64_image = encode_image(image)
image_content = {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{base64_image}"}}

try:
chat_completion = client.chat.completions.create(
messages=[
{
"role": "user",
"content": [
{"type": "text", "text": prompt},
image_content,
],
}
],
model=LLAVA_MODEL,
)
return chat_completion.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"

def check_content_safety(image_description, api_key):
client = Groq(api_key=api_key)

try:
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a content safety classifier. Analyze the given text and determine if it contains any unsafe or inappropriate content."},
{"role": "user", "content": f"Please analyze this image description for any unsafe or inappropriate content: {image_description}"}
],
model=LLAMA_GUARD_MODEL,
)
return chat_completion.choices[0].message.content
except Exception as e:
return f"Error: {str(e)}"

def process_image(image, url, prompt, api_key):
if image is not None:
return analyze_image(image, prompt, api_key), check_content_safety(analyze_image(image, prompt, api_key), api_key)
elif url:
try:
response = requests.get(url)
image = Image.open(io.BytesIO(response.content))
return analyze_image(url, prompt, api_key, is_url=True), check_content_safety(analyze_image(url, prompt, api_key, is_url=True), api_key)
except:
return "Invalid image URL. Please provide a direct link to an image.", ""
else:
return "Please provide an image to analyze.", ""

def launch():
with gr.Blocks() as demo:
gr.Markdown("# Image Analysis with Groq API")

with gr.Row():
with gr.Column():
api_key = gr.Textbox(label="Enter your Groq API Key:", type="password")
image_input = gr.Image(type="pil", label="Upload Image")
url_input = gr.Textbox(label="Or paste image URL")
prompt = gr.Textbox(label="Enter a prompt for image analysis:", value="Describe the contents of this image in detail.")
analyze_button = gr.Button("Analyze Image")

with gr.Column():
analysis_output = gr.Textbox(label="Analysis Result")
safety_output = gr.Textbox(label="Content Safety Check")

analyze_button.click(
fn=process_image,
inputs=[image_input, url_input, prompt, api_key],
outputs=[analysis_output, safety_output]
)

demo.launch()

if __name__ == "__main__":
launch()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
self.__INTERCEPTION_ROUTE_REWRITE_MANIFEST="[]"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 3,
"middleware": {},
"functions": {},
"sortedMiddleware": []
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"node": {},
"edge": {},
"encryptionKey": "RsP/1g1afXOf67tygDO4r89ef0cmb0oL2lgcE0BofgA="
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"type": "module"}
14 changes: 11 additions & 3 deletions tutorials/parallel-tool-use/parallel-tool-use.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@
"id": "67e90d90",
"metadata": {},
"source": [
"## Parallel Tool Use with Groq API 💪\n",
"Tool use support is available for all text-to-text models and parallel tool use support is enabled for all Llama 3 and Llama 3.1 models. For a better tool use experience, Groq also released two open-source models fine-tuned for tool use in collaboration with [Glaive AI](https://glaive.ai/):\n",
"## Parallel Tool Use and Function Calling with Groq API 💪\n",
"Tool use, or function calling, support is available for all text models and parallel tool use support is enabled for all Llama 3 and Llama 3.1 models. The Llama 3.1 models now support the native tool use format that was used in post-training, which results in much better quality, especially in multi-turn conversations and parallel tool calling.\n",
"\n",
"Groq also released two open-source models fine-tuned for tool use in collaboration with [Glaive AI](https://glaive.ai/), which are available on HuggingFace and via Groq API for your applications:\n",
"- [groq/Llama-3-Groq-70B-Tool-Use](https://huggingface.co/Groq/Llama-3-Groq-70B-Tool-Use)\n",
"- [groq/Llama-3-Groq-8B-Tool-Use](https://huggingface.co/Groq/Llama-3-Groq-8B-Tool-Use)\n",
"\n",
"These models are available on HuggingFace and via Groq API for your applications! Read more about the training approach and benchmarking results [here](https://groq.com/introducing-llama-3-groq-tool-use-models/). \n",
"Read more about the training approach and benchmarking results [here](https://groq.com/introducing-llama-3-groq-tool-use-models/). \n",
"\n",
"For your applications that require tool use, we highly recommend trying the following models:\n",
"- `llama3-groq-70b-8192-tool-use-preview`\n",
"- `llama3-groq-8b-8192-tool-use-preview`\n",
"- `llama-3.1-70b-versatile`\n",
"- `llama-3.1-8b-instant`\n",
"\n",
"In this basic tutorial, we will be reviewing the basic structure of parallel tool use with Groq API to fetch given product pricing for multiple bakery items.\n"
]
Expand Down

0 comments on commit 7caf0fa

Please sign in to comment.