import requests import json class BingChatAPI: def __init__(self, base_url="https://devsdocode-bing.hf.space"): self.base_url = base_url def generate(self, conversation_history, realtime=False, md_format=True): """ Generate a response using the Bing Chat API. Args: conversation_history (list or str): A list of message objects or a string query. realtime (bool): Whether to stream the response in real-time. md_format (bool): Whether to format the response in Markdown. Returns: dict: The API response. """ endpoint = f"{self.base_url}/generate" if isinstance(conversation_history, str): conversation_history = [{"role": "user", "content": conversation_history}] params = { "conversation_history": json.dumps(conversation_history), "realtime": str(realtime).lower(), "md_format": str(md_format).lower() } response = requests.get(endpoint, params=params) response.raise_for_status() # Raise an exception for bad status codes return response.json() def get_available_models(self): """Get the list of available models.""" endpoint = f"{self.base_url}/available_models" response = requests.get(endpoint) response.raise_for_status() return response.json() def get_endpoints(self): """Get the list of available endpoints.""" endpoint = f"{self.base_url}/endpoints" response = requests.get(endpoint) response.raise_for_status() return response.json() def get_developer_info(self): """Get the developer information.""" endpoint = f"{self.base_url}/developer_info" response = requests.get(endpoint) response.raise_for_status() return response.json() # Example usage if __name__ == "__main__": api = BingChatAPI() # Generate a response conversation = [{"role": "user", "content": "Who is Devs Do Code"}] response = api.generate(conversation) print("Generated Response:", response) # Get available models models = api.get_available_models() print("Available Models:", models) # Get endpoints endpoints = api.get_endpoints() print("Endpoints:", endpoints) # Get developer info dev_info = api.get_developer_info() print("Developer Info:", dev_info)