# chat.py

import os
import openai

# Replace with your actual OpenAI API key or environment variable lookup
openai.api_key = os.environ.get("OPENAI_API_KEY") or "YOUR_OPENAI_API_KEY"

# Replace with the actual Qwen model identifier.
QWEN_MODEL = "Qwen/Qwen-7B-Chat"


def chat_with_qwen(prompt: str, temperature: float = 0.7, max_tokens: int = 256) -> str:
    """Interacts with the Qwen model to generate a response.

    Args:
        prompt: The prompt to send to the model.
        temperature: The sampling temperature.
        max_tokens: The maximum number of tokens to generate.

    Returns:
        The generated response from the model.
    """
    try:
        completion = openai.Completion.create(
            engine=QWEN_MODEL,
            prompt=prompt,
            temperature=temperature,
            max_tokens=max_tokens,
            n=1,
            stop=None,  # Or a list of stop sequences
        )
        return completion.choices[0].text.strip()
    except Exception as e:
        return f"Error: {e}"

if __name__ == '__main__':
    # Example usage (for testing)
    response = chat_with_qwen("Explain the concept of quantum entanglement.")
    print(response)
