Team Ai
Apppublic

Tiat/context7-server

sourceHugging Faceupdated 1y agoView on Hugging Face
1likes
example_usage.py56 linesDownload Raw Back to root
1#!/usr/bin/env python32"""3Example usage of the Context7 OpenAI-compatible API server4"""5 6import openai7import os8 9# Configure the OpenAI client to use our Context7 server10# Replace the base_url with your actual Hugging Face Space URL11client = openai.OpenAI(12    api_key="dummy-key",  # Not required for our server, but OpenAI client needs it13    base_url="https://your-username-context7-server.hf.space/v1"  # Replace with your Space URL14)15 16def ask_question(question: str):17    """Ask a question using the Context7 server"""18    try:19        response = client.chat.completions.create(20            model="context7-mcp",21            messages=[22                {"role": "user", "content": question}23            ],24            max_tokens=100025        )26        27        return response.choices[0].message.content28    except Exception as e:29        return f"Error: {e}"30 31# Example questions32questions = [33    "How do I create a FastAPI application?",34    "What are React hooks and how do I use them?",35    "How do I connect to MongoDB in Python?",36    "What is the difference between Next.js and React?",37    "How do I create a Docker container for a Python app?"38]39 40if __name__ == "__main__":41    print("Context7 OpenAI-Compatible API Server Example")42    print("=" * 50)43    44    for i, question in enumerate(questions, 1):45        print(f"\n{i}. Question: {question}")46        print("-" * 40)47        48        answer = ask_question(question)49        print(f"Answer: {answer[:300]}...")  # Show first 300 characters50        51        if i < len(questions):52            input("\nPress Enter to continue to the next question...")53    54    print("\n\nExample completed!")55    print("You can modify the questions list or ask your own questions.")56