Skip to content

Using CURL for OpenAI API

%%bash
curl -X POST https://api.openai.com/v1/chat/completions \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -d '{
    "model": "gpt-4o",
    "messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "How do I get better at programming?"
      }
    ]
  }'

Using CURL for Groq Cloud API

%%bash
curl -X POST "https://api.groq.com/openai/v1/chat/completions" \
     -H "Authorization: Bearer $GROQ_API_KEY" \
     -H "Content-Type: application/json" \
     -d '{"messages": [
      {
        "role": "system",
        "content": "You are a helpful assistant."
      },
      {
        "role": "user",
        "content": "How do I get better at programming?"
      }
    ], 
    "model": "llama3-70b-8192"}'

Setting up

%pip install groq -q

Basic Completion and Chat

import os
from groq import Groq
from IPython.display import display, Markdown

client = Groq(
    api_key=os.environ.get("GROQ_API_KEY"),
)

chat_completion = client.chat.completions.create(
    messages=[
        {"role": "system", "content": "You are a professional Data Scientist."},
        {"role": "user", "content": "Can you explain how the neural networks work?"},
    ],
    model="llama3-70b-8192",
)

Markdown(chat_completion.choices[0].message.content)

Streaming Chat Completion

chat_streaming = client.chat.completions.create(
    messages=[
        {"role": "system", "content": "You are a monk from Thailand."},
        {"role": "user", "content": "Can you explain the meaning of life?"},
    ],
    model="llama3-70b-8192",
    temperature=0.3,
    max_tokens=360,
    top_p=1,
    stop=None,
    stream=True,
)

for chunk in chat_streaming:
    print(chunk.choices[0].delta.content, end="")

Async Chat Completion

import asyncio
from groq import AsyncGroq

client = AsyncGroq()


async def main():

    chat_completion = await client.chat.completions.create(
        messages=[
            {"role": "system", "content": "You are a psychiatrist helping young minds"},
            {
                "role": "user",
                "content": "I panicked during the test, even though I knew everything on the test paper.",
            },
        ],
        model="llama3-70b-8192",
        temperature=0.3,
        max_tokens=360,
        top_p=1,
        stop=None,
        stream=False,
    )

    print(chat_completion.choices[0].message.content)


await main()  # for Python file use asyncio.run(main())

Streaming an Async Chat Completion

import asyncio
from groq import AsyncGroq

client = AsyncGroq()


async def main():

    chat_streaming = await client.chat.completions.create(
        messages=[
            {"role": "system", "content": "You are a psychiatrist helping young minds"},
            {
                "role": "user",
                "content": "I panicked during the test, even though I knew everything on the test paper.",
            },
        ],
        model="llama3-70b-8192",
        temperature=0.3,
        max_tokens=360,
        top_p=1,
        stop=None,
        stream=True,
    )
    async for chunk in chat_streaming:
            print(chunk.choices[0].delta.content, end="")

await main()  # for Python file use asyncio.run(main())

RAG with Llamaindex