Spaces:
Runtime error
Runtime error
File size: 2,558 Bytes
1dccd50 16e513e 1dccd50 16e513e 1dccd50 16e513e 1dccd50 16e513e |
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 |
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) |