File size: 3,701 Bytes
a91be8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import os
from pathlib import Path
from groq import Groq
from google import genai # Using the specific import you requested
from dotenv import load_dotenv

def test_groq_connection():
    """
    Loads the Groq API key from a .env file and tests the endpoint
    with a simple streaming query.
    """
    # 1. Build a reliable path to the .env file
    # This finds the script's directory, goes up to the project root,
    # and then into the 'configs' folder.
    script_dir = Path(__file__).parent
    project_root = script_dir.parent
    dotenv_path = project_root / "configs" / ".env"
    load_dotenv(dotenv_path=dotenv_path)

    api_key = os.getenv("GROQ_API_KEY")
    if not api_key:
        print(f"πŸ”΄ Error: GROQ_API_KEY not found.")
        print(f"Please ensure it is set in your {dotenv_path} file.")
        return

    print("βœ… Groq API key loaded successfully.")

    try:
        # 2. Initialize the Groq client
        client = Groq()
        print("πŸ€– Initialized Groq client. Sending a test query...")

        # 3. Create a test chat completion request
        completion = client.chat.completions.create(
            model="llama-3.1-8b-instant",
            messages=[
                {
                    "role": "user",
                    "content": "Explain why low-latency is important for LLMs in one short sentence."
                }
            ],
            temperature=0.7,
            max_tokens=1024,
            top_p=1,
            stream=True,
            stop=None,
        )

        # 4. Print the streamed response from the model
        print("\nπŸ“ Groq API Response:")
        print("-" * 20)
        for chunk in completion:
            print(chunk.choices[0].delta.content or "", end="")
        print("\n" + "-" * 20)
        print("\nβœ… Test successful! The Groq endpoint is working.")

    except Exception as e:
        print(f"πŸ”΄ An error occurred during the Groq API call: {e}")

def test_gemini_connection():
    """
    Loads the Google Gemini API key from a .env file and tests the endpoint
    using the genai.Client pattern.
    """
    # 1. Build a reliable path to the .env file (assuming same location)
    script_dir = Path(__file__).parent
    project_root = script_dir.parent
    dotenv_path = project_root / "configs" / ".env"
    load_dotenv(dotenv_path=dotenv_path)

    api_key = os.getenv("GOOGLE_API_KEY")
    if not api_key:
        print(f"πŸ”΄ Error: GOOGLE_API_KEY not found.")
        print(f"Please ensure it is set in your {dotenv_path} file.")
        return

    print("βœ… Google API key loaded successfully.")

    try:
        # 2. Initialize the Gemini client using the specified pattern
        client = genai.Client(api_key=api_key)
        print("πŸ€– Initialized Gemini client. Sending a test query...")

        # 3. Send a test prompt using the client.models.generate_content method
        response = client.models.generate_content(
            model="gemini-2.5-flash", # Using the qualified model name
            contents="Explain the importance of APIs in one short sentence."
        )

        # 4. Print the response
        print("\nπŸ“ Gemini API Response:")
        print("-" * 20)
        print(response.text)
        print("-" * 20)
        print("\nβœ… Test successful! The Gemini endpoint is working.")

    except Exception as e:
        print(f"πŸ”΄ An error occurred during the Gemini API call: {e}")


# Run the test functions when the script is executed
if __name__ == "__main__":
    print("--- Running Groq API Connection Test ---")
    test_groq_connection()
    print("\n" + "="*40 + "\n")
    print("--- Running Gemini API Connection Test ---")
    test_gemini_connection()