Spaces:
Running
Running
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, convo_tone="Balanced"): | |
""" | |
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. | |
convo_tone (str): The desired conversational style or tone. | |
- Balanced | |
- Creative | |
- Precise | |
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(), | |
"convo_tone": convo_tone | |
} | |
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": "My name is Devs Do Code"}, | |
{"role": "assistant", "content": "**Devs Do Code** is a platform dedicated to providing resources and tutorials on AI and Python programming. His Owner is Sreejan. They have a YouTube channel where they share unique Python projects, tutorials, and showcases of their AI developments[^1^]. One of their notable projects is **AI4Free**, a Python library that offers free access to various large language models without requiring API keys or fees[^2^].\n\nYou can explore their content on their [YouTube channel](https://www.youtube.com/@DevsDoCode) and check out their projects on [GitHub](https://github.com/Devs-Do-Code)[^1^][^2^]. If you're interested in AI and Python, their resources could be quite valuable!"}, | |
{"role": "user", "content": "What is the Owner?"} | |
] | |
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) |