File size: 12,098 Bytes
eb6ddbd
 
2201358
 
12afc5d
2201358
12afc5d
2201358
12afc5d
eb6ddbd
2201358
eb6ddbd
 
 
12afc5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
eb6ddbd
 
2201358
eb6ddbd
2201358
 
eb6ddbd
 
 
 
 
 
 
 
 
 
 
 
 
 
2201358
eb6ddbd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2201358
 
eb6ddbd
2201358
eb6ddbd
2201358
eb6ddbd
 
2201358
 
12afc5d
eb6ddbd
2201358
eb6ddbd
 
2201358
 
12afc5d
 
2201358
 
12afc5d
 
 
 
 
2201358
12afc5d
 
 
2201358
 
 
 
12afc5d
 
 
 
 
2201358
12afc5d
2201358
12afc5d
 
 
2201358
 
 
 
12afc5d
2201358
 
12afc5d
 
 
2201358
12afc5d
2201358
12afc5d
 
 
 
 
 
 
 
2201358
eb6ddbd
2201358
 
eb6ddbd
2201358
 
eb6ddbd
 
2201358
eb6ddbd
 
 
 
 
2201358
eb6ddbd
 
12afc5d
2201358
12afc5d
 
 
 
 
2201358
12afc5d
2201358
12afc5d
 
 
2201358
12afc5d
 
2201358
 
eb6ddbd
 
 
 
 
2201358
eb6ddbd
 
 
 
2201358
eb6ddbd
 
 
2201358
eb6ddbd
 
12afc5d
 
 
2201358
12afc5d
 
 
 
 
 
2201358
12afc5d
2201358
12afc5d
 
 
 
2201358
12afc5d
 
 
 
2201358
12afc5d
2201358
eb6ddbd
 
 
 
 
2201358
12afc5d
eb6ddbd
 
 
2201358
eb6ddbd
 
12afc5d
 
 
2201358
12afc5d
 
 
 
 
 
2201358
12afc5d
2201358
12afc5d
 
 
 
2201358
12afc5d
 
 
 
2201358
12afc5d
eb6ddbd
 
2201358
eb6ddbd
2201358
eb6ddbd
 
 
2201358
eb6ddbd
 
2201358
 
eb6ddbd
2201358
 
 
eb6ddbd
 
 
 
 
12afc5d
 
 
2201358
eb6ddbd
 
 
12afc5d
 
eb6ddbd
 
2201358
eb6ddbd
2201358
eb6ddbd
 
2201358
eb6ddbd
 
 
2201358
 
 
 
 
eb6ddbd
 
2201358
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
import os
import random
from typing import Dict, List

import google.generativeai as genai
import gradio as gr
import openai
from anthropic import Anthropic
from openai import OpenAI  # Add explicit OpenAI import


def get_all_models():
    """Get all available models from the registries."""
    return [
        "SambaNova: Meta-Llama-3.2-1B-Instruct",
        "SambaNova: Meta-Llama-3.2-3B-Instruct",
        "SambaNova: Llama-3.2-11B-Vision-Instruct",
        "SambaNova: Llama-3.2-90B-Vision-Instruct",
        "SambaNova: Meta-Llama-3.1-8B-Instruct",
        "SambaNova: Meta-Llama-3.1-70B-Instruct",
        "SambaNova: Meta-Llama-3.1-405B-Instruct",
        "Hyperbolic: Qwen/Qwen2.5-Coder-32B-Instruct",
        "Hyperbolic: meta-llama/Llama-3.2-3B-Instruct",
        "Hyperbolic: meta-llama/Meta-Llama-3.1-8B-Instruct",
        "Hyperbolic: meta-llama/Meta-Llama-3.1-70B-Instruct",
        "Hyperbolic: meta-llama/Meta-Llama-3-70B-Instruct",
        "Hyperbolic: NousResearch/Hermes-3-Llama-3.1-70B",
        "Hyperbolic: Qwen/Qwen2.5-72B-Instruct",
        "Hyperbolic: deepseek-ai/DeepSeek-V2.5",
        "Hyperbolic: meta-llama/Meta-Llama-3.1-405B-Instruct",
    ]


def generate_discussion_prompt(original_question: str, previous_responses: List[str]) -> str:
    """Generate a prompt for models to discuss and build upon previous
    responses."""
    prompt = f"""You are participating in a multi-AI discussion about this question: "{original_question}"

Previous responses from other AI models:
{chr(10).join(f"- {response}" for response in previous_responses)}

Please provide your perspective while:
1. Acknowledging key insights from previous responses
2. Adding any missing important points
3. Respectfully noting if you disagree with anything and explaining why
4. Building towards a complete answer

Keep your response focused and concise (max 3-4 paragraphs)."""
    return prompt


def generate_consensus_prompt(original_question: str, discussion_history: List[str]) -> str:
    """Generate a prompt for final consensus building."""
    return f"""Review this multi-AI discussion about: "{original_question}"

Discussion history:
{chr(10).join(discussion_history)}

As a final synthesizer, please:
1. Identify the key points where all models agreed
2. Explain how any disagreements were resolved
3. Present a clear, unified answer that represents our collective best understanding
4. Note any remaining uncertainties or caveats

Keep the final consensus concise but complete."""


def chat_with_openai(model: str, messages: List[Dict], api_key: str | None) -> str:
    import openai

    client = openai.OpenAI(api_key=api_key)
    response = client.chat.completions.create(model=model, messages=messages)
    return response.choices[0].message.content


def chat_with_anthropic(messages: List[Dict], api_key: str | None) -> str:
    """Chat with Anthropic's Claude model."""
    client = Anthropic(api_key=api_key)
    response = client.messages.create(model="claude-3-sonnet-20240229", messages=messages, max_tokens=1024)
    return response.content[0].text


def chat_with_gemini(messages: List[Dict], api_key: str | None) -> str:
    """Chat with Gemini Pro model."""
    genai.configure(api_key=api_key)
    model = genai.GenerativeModel("gemini-pro")

    # Convert messages to Gemini format
    gemini_messages = []
    for msg in messages:
        role = "user" if msg["role"] == "user" else "model"
        gemini_messages.append({"role": role, "parts": [msg["content"]]})

    response = model.generate_content([m["parts"][0] for m in gemini_messages])
    return response.text


def chat_with_sambanova(
    messages: List[Dict], api_key: str | None, model_name: str = "Llama-3.2-90B-Vision-Instruct"
) -> str:
    """Chat with SambaNova's models using their OpenAI-compatible API."""
    client = openai.OpenAI(
        api_key=api_key,
        base_url="https://api.sambanova.ai/v1",
    )

    response = client.chat.completions.create(
        model=model_name, messages=messages, temperature=0.1, top_p=0.1  # Use the specific model name passed in
    )
    return response.choices[0].message.content


def chat_with_hyperbolic(
    messages: List[Dict], api_key: str | None, model_name: str = "Qwen/Qwen2.5-Coder-32B-Instruct"
) -> str:
    """Chat with Hyperbolic's models using their OpenAI-compatible API."""
    client = OpenAI(api_key=api_key, base_url="https://api.hyperbolic.xyz/v1")

    # Add system message to the start of the messages list
    full_messages = [
        {"role": "system", "content": "You are a helpful assistant. Be descriptive and clear."},
        *messages,
    ]

    response = client.chat.completions.create(
        model=model_name,  # Use the specific model name passed in
        messages=full_messages,
        temperature=0.7,
        max_tokens=1024,
    )
    return response.choices[0].message.content


def multi_model_consensus(
    question: str, selected_models: List[str], rounds: int = 3, progress: gr.Progress = gr.Progress()
) -> list[tuple[str, str]]:
    if not selected_models:
        raise gr.Error("Please select at least one model to chat with.")

    chat_history = []
    discussion_history = []

    # Initial responses
    progress(0, desc="Getting initial responses...")
    initial_responses = []
    for i, model in enumerate(selected_models):
        provider, model_name = model.split(": ", 1)

        try:
            if provider == "Anthropic":
                api_key = os.getenv("ANTHROPIC_API_KEY")
                response = chat_with_anthropic(messages=[{"role": "user", "content": question}], api_key=api_key)
            elif provider == "SambaNova":
                api_key = os.getenv("SAMBANOVA_API_KEY")
                response = chat_with_sambanova(
                    messages=[
                        {"role": "system", "content": "You are a helpful assistant"},
                        {"role": "user", "content": question},
                    ],
                    api_key=api_key,
                )
            elif provider == "Hyperbolic":  # Add Hyperbolic case
                api_key = os.getenv("HYPERBOLIC_API_KEY")
                response = chat_with_hyperbolic(messages=[{"role": "user", "content": question}], api_key=api_key)
            else:  # Gemini
                api_key = os.getenv("GEMINI_API_KEY")
                response = chat_with_gemini(messages=[{"role": "user", "content": question}], api_key=api_key)

            initial_responses.append(f"{model}: {response}")
            discussion_history.append(f"Initial response from {model}:\n{response}")
            chat_history.append((f"Initial response from {model}", response))
        except Exception as e:
            chat_history.append((f"Error from {model}", str(e)))

    # Discussion rounds
    for round_num in range(rounds):
        progress((round_num + 1) / (rounds + 2), desc=f"Discussion round {round_num + 1}...")
        round_responses = []

        random.shuffle(selected_models)  # Randomize order each round
        for model in selected_models:
            provider, model_name = model.split(": ", 1)

            try:
                discussion_prompt = generate_discussion_prompt(question, discussion_history)
                if provider == "Anthropic":
                    api_key = os.getenv("ANTHROPIC_API_KEY")
                    response = chat_with_anthropic(
                        messages=[{"role": "user", "content": discussion_prompt}], api_key=api_key
                    )
                elif provider == "SambaNova":
                    api_key = os.getenv("SAMBANOVA_API_KEY")
                    response = chat_with_sambanova(
                        messages=[
                            {"role": "system", "content": "You are a helpful assistant"},
                            {"role": "user", "content": discussion_prompt},
                        ],
                        api_key=api_key,
                    )
                elif provider == "Hyperbolic":  # Add Hyperbolic case
                    api_key = os.getenv("HYPERBOLIC_API_KEY")
                    response = chat_with_hyperbolic(
                        messages=[{"role": "user", "content": discussion_prompt}], api_key=api_key
                    )
                else:  # Gemini
                    api_key = os.getenv("GEMINI_API_KEY")
                    response = chat_with_gemini(
                        messages=[{"role": "user", "content": discussion_prompt}], api_key=api_key
                    )

                round_responses.append(f"{model}: {response}")
                discussion_history.append(f"Round {round_num + 1} - {model}:\n{response}")
                chat_history.append((f"Round {round_num + 1} - {model}", response))
            except Exception as e:
                chat_history.append((f"Error from {model} in round {round_num + 1}", str(e)))

    # Final consensus
    progress(0.9, desc="Building final consensus...")
    model = selected_models[0]
    provider, model_name = model.split(": ", 1)

    try:
        consensus_prompt = generate_consensus_prompt(question, discussion_history)
        if provider == "Anthropic":
            api_key = os.getenv("ANTHROPIC_API_KEY")
            final_consensus = chat_with_anthropic(
                messages=[{"role": "user", "content": consensus_prompt}], api_key=api_key
            )
        elif provider == "SambaNova":
            api_key = os.getenv("SAMBANOVA_API_KEY")
            final_consensus = chat_with_sambanova(
                messages=[
                    {"role": "system", "content": "You are a helpful assistant"},
                    {"role": "user", "content": consensus_prompt},
                ],
                api_key=api_key,
            )
        elif provider == "Hyperbolic":  # Add Hyperbolic case
            api_key = os.getenv("HYPERBOLIC_API_KEY")
            final_consensus = chat_with_hyperbolic(
                messages=[{"role": "user", "content": consensus_prompt}], api_key=api_key
            )
        else:  # Gemini
            api_key = os.getenv("GEMINI_API_KEY")
            final_consensus = chat_with_gemini(
                messages=[{"role": "user", "content": consensus_prompt}], api_key=api_key
            )
    except Exception as e:
        final_consensus = f"Error getting consensus from {model}: {str(e)}"

    chat_history.append(("Final Consensus", final_consensus))

    progress(1.0, desc="Done!")
    return chat_history


with gr.Blocks() as demo:
    gr.Markdown("# Experimental Multi-Model Consensus Chat")
    gr.Markdown(
        """Select multiple models to collaborate on answering your question.
                The models will discuss with each other and attempt to reach a consensus.
                Maximum 3 models can be selected at once."""
    )

    with gr.Row():
        with gr.Column():
            model_selector = gr.Dropdown(
                choices=get_all_models(),
                multiselect=True,
                label="Select Models (max 3)",
                info="Choose up to 3 models to participate in the discussion",
                value=["SambaNova: Llama-3.2-90B-Vision-Instruct", "Hyperbolic: Qwen/Qwen2.5-Coder-32B-Instruct"],
                max_choices=3,
            )
            rounds_slider = gr.Slider(
                minimum=1,
                maximum=2,
                value=1,
                step=1,
                label="Discussion Rounds",
                info="Number of rounds of discussion between models",
            )

    chatbot = gr.Chatbot(height=600, label="Multi-Model Discussion")
    msg = gr.Textbox(label="Your Question", placeholder="Ask a question for the models to discuss...")

    def respond(message, selected_models, rounds):
        chat_history = multi_model_consensus(message, selected_models, rounds)
        return chat_history

    msg.submit(respond, [msg, model_selector, rounds_slider], [chatbot], api_name="consensus_chat")

for fn in demo.fns.values():
    fn.api_name = False

if __name__ == "__main__":
    demo.launch()