File size: 3,644 Bytes
1dccd50
16e513e
1dccd50
16e513e
 
 
1dccd50
1094e6e
16e513e
 
 
 
 
 
1094e6e
 
 
 
16e513e
 
 
 
 
 
 
 
 
 
 
 
 
1094e6e
 
16e513e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1dccd50
16e513e
 
 
a4fdbef
 
 
 
 
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
78
79
80
81
82
83
84
85
86
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)