This guide walks you through integrating Groq with Cycls to create an asynchronous application that can process and respond to user messages using Groq’s API.

Prerequisites

To be able to use Groq’s API, you first need to create an account in Groq. Follow the steps below to register and get your API Key:

  1. Go to https://console.groq.com/login and Create your account or login.
  2. Access GroqCloud, and navigate to API Keys.
  3. Click on the Create API Key button to create a new API Key.
  4. Copy and save your new API Key, as Groq won’t allow you to see it in the future.
  5. Once you have the API Key you need to set it to a .env file:
GROQ_API_KEY=<YOUR_API_KEY>

Integrating with Groq

By following these steps, you can integrate Groq with Cycls to create an asynchronous application that processes and responds to user messages using Groq’s API.

1

Set Up Your Environment

First, install the necessary dependencies. Make sure you have the dotenv package to load environment variables and the groq package for interacting with the Groq API.

pip install python-dotenv groq
2

Import Required Modules

Import the necessary modules, including AsyncApp from Cycls, load_dotenv from dotenv, and Groq from the groq package. Also, import os to access environment variables.

from cycls import AsyncApp
from dotenv import load_dotenv
import os
from groq import Groq
3

Load Environment Variables

Use load_dotenv to load your environment variables from a .env file. Ensure you have CYCLS_SECRET_KEY and GROQ_API_KEY defined in your .env file.

load_dotenv()

secret = os.getenv("CYCLS_SECRET_KEY")
groq_api_key = os.getenv("GROQ_API_KEY")
4

Initialize the Groq Client

Create an instance of the Groq client using the API key from your environment variables.

client = Groq(api_key=groq_api_key)
5

Initialize the AsyncApp

Create an instance of AsyncApp with the necessary parameters.

app = AsyncApp(
    secret=secret,
    handler="@your-handler",
)
6

Define the Entry Point

Define the entry point function for your application. This function will process incoming messages and use the Groq client to generate a response.

@app
async def entry_point(context):
    received_message = context.message.content.text

    groq_chat_response = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": received_message,
            }
        ],
        model="llama3-8b-8192",
    )

    await context.send.text(groq_chat_response.choices[0].message.content)
7

Publish Your App

Finally, publish your app by calling the publish method.

app.publish()
8

Run Your App

Run your app in the directory containing main.py:

python main.py

Complete Example

Here is the complete code for integrating Groq with Cycls:

from cycls import AsyncApp
from dotenv import load_dotenv
from groq import Groq
import os

load_dotenv()

secret = os.getenv("CYCLS_SECRET_KEY")
groq_api_key = os.getenv("GROQ_API_KEY")

app = AsyncApp(
    secret=secret,
    handler="@your-handler",
)

client = Groq(api_key=groq_api_key)

@app
async def entry_point(context):
    received_message = context.message.content.text

    groq_chat_response = client.chat.completions.create(
        messages=[
            {
                "role": "user",
                "content": received_message,
            }
        ],
        model="llama3-8b-8192",
    )

    await context.send.text(groq_chat_response.choices[0].message.content)

app.publish()