File size: 28,770 Bytes
deb6f27
f5b302e
b929813
 
 
 
 
 
 
f5b302e
deb6f27
b929813
 
 
 
f5b302e
b929813
 
 
 
 
 
 
 
 
 
f5b302e
b929813
f5b302e
 
b929813
 
 
 
deb6f27
 
b929813
 
 
 
 
 
 
 
 
f5b302e
b929813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa1bef5
 
 
 
b929813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
deb6f27
b929813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa1bef5
b929813
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa1bef5
 
 
 
 
 
b929813
 
 
 
 
 
 
 
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
from transformers import pipeline
import json
import argparse
import os
import sys
import subprocess
import requests
from typing import List, Dict, Any, Optional, Union
import time


# Check for Hugging Face token
def check_hf_token():
    """Check if a Hugging Face token is properly set up."""
    token = os.environ.get("HUGGING_FACE_HUB_TOKEN") or os.environ.get("HF_TOKEN")

    if not token:
        print("\nWarning: No Hugging Face token found in environment variables.")
        print(
            "To use gated models like Gemma, you need to set up a token with the right permissions."
        )
        print("1. Create a token at https://huggingface.co/settings/tokens")
        print("2. Make sure to enable 'Access to public gated repositories'")
        print("3. Set it as an environment variable:")
        print("   export HUGGING_FACE_HUB_TOKEN=your_token_here")
        return False

    return True


def load_social_graph(file_path="social_graph.json"):
    """Load the social graph from a JSON file."""
    with open(file_path, "r") as f:
        return json.load(f)


def get_person_info(social_graph, person_id):
    """Get information about a person from the social graph."""
    if person_id in social_graph["people"]:
        return social_graph["people"][person_id]
    else:
        available_people = ", ".join(social_graph["people"].keys())
        raise ValueError(
            f"Person '{person_id}' not found in social graph. Available people: {available_people}"
        )


def build_enhanced_prompt(social_graph, person_id, topic=None, user_message=None):
    """Build an enhanced prompt using social graph information."""
    # Get AAC user information
    aac_user = social_graph["aac_user"]

    # Get conversation partner information
    person = get_person_info(social_graph, person_id)

    # Start building the prompt with AAC user information
    prompt = f"""I am {aac_user['name']}, a {aac_user['age']}-year-old with MND (Motor Neuron Disease) from {aac_user['location']}.
{aac_user['background']}

My communication needs: {aac_user['communication_needs']}

I am talking to {person['name']}, who is my {person['role']}.
About {person['name']}: {person['context']}
We typically talk about: {', '.join(person['topics'])}
We communicate {person['frequency']}.
"""

    # Add places information if available
    if "places" in social_graph:
        relevant_places = social_graph["places"][
            :3
        ]  # Just use a few places for context
        prompt += f"\nPlaces important to me: {', '.join(relevant_places)}\n"

    # Add communication style based on relationship
    if person["role"] in ["wife", "son", "daughter", "mother", "father"]:
        prompt += "I communicate with my family in a warm, loving way, sometimes using inside jokes.\n"
    elif person["role"] in ["doctor", "therapist", "nurse"]:
        prompt += (
            "I communicate with healthcare providers in a direct, informative way.\n"
        )
    elif person["role"] in ["best mate", "friend"]:
        prompt += "I communicate with friends casually, often with humor and sometimes swearing.\n"
    elif person["role"] in ["work colleague", "boss"]:
        prompt += "I communicate with colleagues professionally but still friendly.\n"

    # Add common utterances by category if available
    if "common_utterances" in social_graph:
        # Try to find relevant utterance category based on topic
        utterance_category = None
        if topic == "football" or topic == "sports":
            utterance_category = "sports_talk"
        elif topic == "programming" or topic == "tech news":
            utterance_category = "tech_talk"
        elif topic in ["family plans", "children's activities"]:
            utterance_category = "family_talk"

        # Add relevant utterances if category exists
        if (
            utterance_category
            and utterance_category in social_graph["common_utterances"]
        ):
            utterances = social_graph["common_utterances"][utterance_category][:2]
            prompt += f"\nI might say things like: {' or '.join(utterances)}\n"

    # Add topic information if provided
    if topic and topic in person["topics"]:
        prompt += f"\nWe are currently discussing {topic}.\n"

        # Add specific context about this topic with this person
        if topic == "football" and "Manchester United" in person["context"]:
            prompt += (
                "We both support Manchester United and often discuss recent matches.\n"
            )
        elif topic == "programming" and "software developer" in person["context"]:
            prompt += (
                "We both work in software development and share technical interests.\n"
            )
        elif topic == "family plans" and person["role"] in ["wife", "husband"]:
            prompt += "We make family decisions together, considering my condition.\n"
        elif topic == "old scout adventures" and person["role"] == "best mate":
            prompt += "We often reminisce about our Scout camping trips in South East London.\n"
        elif topic == "cycling" and "cycling" in person["context"]:
            prompt += "I miss being able to cycle but enjoy talking about past cycling adventures.\n"

        # Add shared experiences based on relationship and topic
        if person["role"] == "best mate" and topic in ["football", "pub quizzes"]:
            prompt += (
                "We've watched many matches together and done countless pub quizzes.\n"
            )
        elif person["role"] == "wife" and topic in ["family plans", "weekend outings"]:
            prompt += "Emma has been amazing at keeping family life as normal as possible despite my condition.\n"
        elif person["role"] == "son" and topic == "football":
            prompt += "I try to stay engaged with Billy's football enthusiasm even as my condition progresses.\n"

    # Add the user's message if provided
    if user_message:
        prompt += f"\n{person['name']} just said to me: \"{user_message}\"\n"
    else:
        # Use a common phrase from the person if no message is provided
        if person["common_phrases"]:
            default_message = person["common_phrases"][0]
            prompt += f"\n{person['name']} just said to me: \"{default_message}\"\n"

    # Add the response prompt with specific guidance
    prompt += f"""
I want to respond to {person['name']} in a way that is natural, brief (1-2 sentences), and directly relevant to what they just said. I'll use casual language with some humor since we're close friends.

My response to {person['name']}:"""

    return prompt


class LLMInterface:
    """Base interface for language model generation."""

    def __init__(self, model_name, max_length=150, temperature=0.9):
        """Initialize the LLM interface.

        Args:
            model_name: Name or path of the model
            max_length: Maximum length of generated text
            temperature: Controls randomness (higher = more random)
        """
        self.model_name = model_name
        self.max_length = max_length
        self.temperature = temperature

    def generate(self, prompt, num_responses=3):
        """Generate responses for the given prompt.

        Args:
            prompt: The prompt to generate responses for
            num_responses: Number of responses to generate

        Returns:
            A list of generated responses
        """
        raise NotImplementedError("Subclasses must implement this method")

    def cleanup_response(self, text):
        """Clean up a generated response.

        Args:
            text: The raw generated text

        Returns:
            Cleaned up text
        """
        # Make sure it's a complete sentence or phrase
        # If it ends abruptly, add an ellipsis
        if text and not any(text.endswith(end) for end in [".", "!", "?", '..."']):
            if text.endswith('"'):
                text = text[:-1] + '..."'
            else:
                text += "..."

        return text


class HuggingFaceInterface(LLMInterface):
    """Interface for Hugging Face Transformers models."""

    def __init__(self, model_name="distilgpt2", max_length=150, temperature=0.9):
        """Initialize the Hugging Face interface."""
        super().__init__(model_name, max_length, temperature)
        try:
            # Check if we're dealing with a gated model
            is_gated_model = any(
                name in model_name for name in ["gemma", "llama", "mistral"]
            )

            # Get token from environment
            import os

            token = os.environ.get("HUGGING_FACE_HUB_TOKEN") or os.environ.get(
                "HF_TOKEN"
            )

            if is_gated_model and token:
                print(f"Using token for gated model: {model_name}")
                from huggingface_hub import login

                login(token=token, add_to_git_credential=False)

                # Explicitly pass token to pipeline
                from transformers import AutoTokenizer, AutoModelForCausalLM

                tokenizer = AutoTokenizer.from_pretrained(model_name, token=token)
                model = AutoModelForCausalLM.from_pretrained(model_name, token=token)
                self.pipeline = pipeline(
                    "text-generation", model=model, tokenizer=tokenizer
                )
            else:
                self.pipeline = pipeline("text-generation", model=model_name)

            print(f"Successfully loaded model: {model_name}")
        except Exception as e:
            print(f"Error loading model {model_name}: {e}")
            if "gated" in str(e).lower() or "403" in str(e):
                print(
                    "\nThis appears to be a gated model that requires authentication."
                )
                print("Please make sure you:")
                print("1. Have accepted the model license on the Hugging Face Hub")
                print(
                    "2. Have created a token with 'Access to public gated repositories' permission"
                )
                print(
                    "3. Have set the token as HUGGING_FACE_HUB_TOKEN environment variable"
                )
                print("\nAlternatively, try using the Ollama backend:")
                print(
                    f"python demo.py --backend ollama --model gemma:7b-it [other args]"
                )
            raise

    def generate(self, prompt, num_responses=3):
        """Generate responses using the Hugging Face pipeline."""
        # Calculate prompt length in tokens (approximate)
        prompt_length = len(prompt.split())

        # Generate the responses
        responses = self.pipeline(
            prompt,
            max_length=prompt_length + self.max_length,
            temperature=self.temperature,
            do_sample=True,
            num_return_sequences=num_responses,
            top_p=0.92,
            top_k=50,
            truncation=True,
        )

        # Extract just the generated parts (not the prompt)
        generated_texts = []
        for resp in responses:
            # Get the text after the prompt
            generated = resp["generated_text"][len(prompt) :].strip()

            # Clean up the response
            generated = self.cleanup_response(generated)

            # Add to our list if it's not empty
            if generated:
                generated_texts.append(generated)

        return generated_texts


class OllamaInterface(LLMInterface):
    """Interface for Ollama models."""

    def __init__(self, model_name="gemma:7b", max_length=150, temperature=0.9):
        """Initialize the Ollama interface."""
        super().__init__(model_name, max_length, temperature)
        # Check if Ollama is installed and the model is available
        try:
            import requests

            response = requests.get("http://localhost:11434/api/tags")
            if response.status_code == 200:
                models = [model["name"] for model in response.json()["models"]]
                if model_name not in models:
                    print(
                        f"Warning: Model {model_name} not found in Ollama. Available models: {', '.join(models)}"
                    )
                    print(f"You may need to run: ollama pull {model_name}")
            print(f"Ollama is available and will use model: {model_name}")
        except Exception as e:
            print(f"Warning: Ollama may not be installed or running: {e}")
            print("You can install Ollama from https://ollama.ai/")

    def generate(self, prompt, num_responses=3):
        """Generate responses using Ollama API."""
        import requests

        generated_texts = []
        for _ in range(num_responses):
            try:
                response = requests.post(
                    "http://localhost:11434/api/generate",
                    json={
                        "model": self.model_name,
                        "prompt": prompt,
                        "temperature": self.temperature,
                        "max_tokens": self.max_length,
                    },
                    stream=False,
                )

                if response.status_code == 200:
                    # Extract the generated text
                    generated = response.json().get("response", "").strip()

                    # Clean up the response
                    generated = self.cleanup_response(generated)

                    # Add to our list if it's not empty
                    if generated:
                        generated_texts.append(generated)
                else:
                    print(f"Error from Ollama API: {response.text}")
            except Exception as e:
                print(f"Error generating with Ollama: {e}")

        return generated_texts


class LLMToolInterface(LLMInterface):
    """Interface for Simon Willison's LLM tool."""

    def __init__(
        self, model_name="gemini-1.5-pro-latest", max_length=150, temperature=0.9
    ):
        """Initialize the LLM tool interface."""
        super().__init__(model_name, max_length, temperature)
        # Check if LLM tool is installed
        try:
            import subprocess

            result = subprocess.run(["llm", "models"], capture_output=True, text=True)
            if result.returncode == 0:
                models = [
                    line.strip() for line in result.stdout.split("\n") if line.strip()
                ]
                print(f"LLM tool is available. Found {len(models)} models.")

                # Check for specific model types
                gemini_models = [
                    m for m in models if "gemini" in m.lower() or "gemma" in m.lower()
                ]
                if gemini_models:
                    print(f"Gemini models available: {', '.join(gemini_models[:3])}...")

                # Check for Ollama models
                ollama_models = [m for m in models if "ollama" in m.lower()]
                if ollama_models:
                    print(f"Ollama models available: {', '.join(ollama_models[:3])}...")

                # Check for MLX models
                mlx_models = [m for m in models if "mlx" in m.lower()]
                if mlx_models:
                    print(f"MLX models available: {', '.join(mlx_models[:3])}...")

                # Check if the specified model is available
                if not any(self.model_name in m for m in models):
                    print(
                        f"Warning: Model '{self.model_name}' not found in available models."
                    )
                    print("You may need to install the appropriate plugin:")
                    if (
                        "gemini" in self.model_name.lower()
                        or "gemma" in self.model_name.lower()
                    ):
                        print("llm install llm-gemini")
                    elif "mlx" in self.model_name.lower():
                        print("llm install llm-mlx")
                    elif "ollama" in self.model_name.lower():
                        print("llm install llm-ollama")
                        model_name = self.model_name
                        if "/" in model_name:
                            model_name = model_name.split("/")[1]
                        print("ollama pull " + model_name)
            else:
                print("Warning: LLM tool may be installed but returned an error.")
        except Exception as e:
            print(f"Warning: Simon Willison's LLM tool may not be installed: {e}")
            print("You can install it with: pip install llm")

    def generate(self, prompt, num_responses=3):
        """Generate responses using the LLM tool."""
        import subprocess
        import os

        # Check for required environment variables
        if "gemini" in self.model_name.lower() or "gemma" in self.model_name.lower():
            if not os.environ.get("GEMINI_API_KEY"):
                print("Warning: GEMINI_API_KEY environment variable not found.")
                print("Gemini API may not work without it.")
        elif "ollama" in self.model_name.lower():
            # Check if Ollama is running
            try:
                import requests

                response = requests.get("http://localhost:11434/api/tags", timeout=2)
                if response.status_code != 200:
                    print("Warning: Ollama server doesn't seem to be running.")
                    print("Start Ollama with: ollama serve")
            except Exception:
                print("Warning: Ollama server doesn't seem to be running.")
                print("Start Ollama with: ollama serve")

        # Determine the appropriate parameter name for max tokens
        if "gemini" in self.model_name.lower() or "gemma" in self.model_name.lower():
            max_tokens_param = "max_output_tokens"
        elif "ollama" in self.model_name.lower():
            max_tokens_param = "num_predict"
        else:
            max_tokens_param = "max_tokens"

        generated_texts = []
        for _ in range(num_responses):
            try:
                # Call the LLM tool
                result = subprocess.run(
                    [
                        "llm",
                        "-m",
                        self.model_name,
                        "-s",
                        f"temperature={self.temperature}",
                        "-s",
                        f"{max_tokens_param}={self.max_length}",
                        prompt,
                    ],
                    capture_output=True,
                    text=True,
                )

                if result.returncode == 0:
                    # Get the generated text
                    generated = result.stdout.strip()

                    # Clean up the response
                    generated = self.cleanup_response(generated)

                    # Add to our list if it's not empty
                    if generated:
                        generated_texts.append(generated)
                else:
                    print(f"Error from LLM tool: {result.stderr}")
            except Exception as e:
                print(f"Error generating with LLM tool: {e}")

        return generated_texts


class MLXInterface(LLMInterface):
    """Interface for MLX-powered models on Mac."""

    def __init__(
        self, model_name="mlx-community/gemma-7b-it", max_length=150, temperature=0.9
    ):
        """Initialize the MLX interface."""
        super().__init__(model_name, max_length, temperature)
        # Check if MLX is installed
        try:
            import importlib.util

            if importlib.util.find_spec("mlx") is not None:
                print("MLX is available for optimized inference on Mac")
            else:
                print("Warning: MLX is not installed. Install with: pip install mlx")
        except Exception as e:
            print(f"Warning: Error checking for MLX: {e}")

    def generate(self, prompt, num_responses=3):
        """Generate responses using MLX."""
        try:
            # Dynamically import MLX to avoid errors on non-Mac platforms
            import mlx.core as mx
            from transformers import AutoTokenizer, AutoModelForCausalLM

            # Load the model and tokenizer
            tokenizer = AutoTokenizer.from_pretrained(self.model_name)
            model = AutoModelForCausalLM.from_pretrained(
                self.model_name, trust_remote_code=True, mx_dtype=mx.float16
            )

            generated_texts = []
            for _ in range(num_responses):
                # Tokenize the prompt
                inputs = tokenizer(prompt, return_tensors="np")

                # Generate
                outputs = model.generate(
                    inputs["input_ids"],
                    max_length=len(inputs["input_ids"][0]) + self.max_length,
                    temperature=self.temperature,
                    do_sample=True,
                    top_p=0.92,
                    top_k=50,
                )

                # Decode the generated tokens
                generated = tokenizer.decode(
                    outputs[0][len(inputs["input_ids"][0]) :], skip_special_tokens=True
                )

                # Clean up the response
                generated = self.cleanup_response(generated)

                # Add to our list if it's not empty
                if generated:
                    generated_texts.append(generated)

            return generated_texts
        except Exception as e:
            print(f"Error generating with MLX: {e}")
            return []


def create_llm_interface(backend, model_name, max_length=150, temperature=0.9):
    """Create an appropriate LLM interface based on the backend.

    Args:
        backend: The backend to use ('hf', 'llm')
        model_name: The name of the model to use
        max_length: Maximum length of generated text
        temperature: Controls randomness (higher = more random)

    Returns:
        An LLM interface instance
    """
    if backend == "hf":
        return HuggingFaceInterface(model_name, max_length, temperature)
    elif backend == "llm":
        return LLMToolInterface(model_name, max_length, temperature)
    else:
        raise ValueError(f"Unknown backend: {backend}")


def generate_response(
    prompt,
    model_name="distilgpt2",
    max_length=150,
    temperature=0.9,
    num_responses=3,
    backend="hf",
):
    """Generate multiple responses using the specified model and backend.

    Args:
        prompt: The prompt to generate responses for
        model_name: The name of the model to use
        max_length: Maximum number of new tokens to generate
        temperature: Controls randomness (higher = more random)
        num_responses: Number of different responses to generate
        backend: The backend to use ('hf', 'ollama', 'llm', 'mlx')

    Returns:
        A list of generated responses
    """
    # Create the appropriate interface
    interface = create_llm_interface(backend, model_name, max_length, temperature)

    # Generate responses
    return interface.generate(prompt, num_responses)


def main():
    # Set up argument parser
    parser = argparse.ArgumentParser(
        description="Generate AAC responses using social graph context"
    )
    parser.add_argument(
        "--person", default="billy", help="Person ID from the social graph"
    )
    parser.add_argument("--topic", help="Topic of conversation")
    parser.add_argument("--message", help="Message from the conversation partner")
    parser.add_argument(
        "--backend",
        default="llm",
        choices=["hf", "llm"],
        help="Backend to use for generation (hf=HuggingFace, "
        "llm=Simon Willison's LLM tool with support for Gemini/MLX/Ollama)",
    )
    parser.add_argument(
        "--model",
        default="gemini-1.5-pro-latest",
        help="Model to use for generation. Recommended models by backend:\n"
        "- hf: 'distilgpt2', 'gpt2-medium', 'google/gemma-2b-it'\n"
        "- llm: 'gemini-1.5-pro-latest', 'gemma-3-27b-it' (requires llm-gemini plugin)\n"
        "       'mlx-community/gemma-7b-it' (requires llm-mlx plugin)\n"
        "       'Ollama: gemma3:4b-it-qat', 'Ollama: llama3:8b' (requires llm-ollama plugin)",
    )
    parser.add_argument(
        "--num_responses", type=int, default=3, help="Number of responses to generate"
    )
    parser.add_argument(
        "--max_length",
        type=int,
        default=150,
        help="Maximum length of generated responses",
    )
    parser.add_argument(
        "--temperature",
        type=float,
        default=0.9,
        help="Temperature for generation (higher = more creative)",
    )
    args = parser.parse_args()

    # Check for token if using HF backend with gated models
    if args.backend == "hf" and any(
        name in args.model for name in ["gemma", "llama", "mistral"]
    ):
        if not check_hf_token():
            print("\nSuggestion: Try using the LLM tool with Gemini API instead:")
            print(
                f"python demo.py --backend llm --model gemini-1.5-pro-latest --person {args.person}"
                + (f' --topic "{args.topic}"' if args.topic else "")
                + (f' --message "{args.message}"' if args.message else "")
            )
            print("\nOr use a non-gated model:")
            print(
                f"python demo.py --backend hf --model gpt2-medium --person {args.person}"
                + (f' --topic "{args.topic}"' if args.topic else "")
                + (f' --message "{args.message}"' if args.message else "")
            )
            print("\nContinuing anyway, but expect authentication errors...\n")

    # Load the social graph
    social_graph = load_social_graph()

    # Build the prompt
    prompt = build_enhanced_prompt(social_graph, args.person, args.topic, args.message)

    print("\n=== PROMPT ===")
    print(prompt)
    print(
        f"\n=== GENERATING RESPONSE USING {args.backend.upper()} BACKEND WITH MODEL {args.model} ==="
    )

    # Generate the responses
    try:
        responses = generate_response(
            prompt,
            args.model,
            max_length=args.max_length,
            num_responses=args.num_responses,
            temperature=args.temperature,
            backend=args.backend,
        )

        print("\n=== RESPONSES ===")
        for i, response in enumerate(responses, 1):
            print(f"{i}. {response}")
            print()
    except Exception as e:
        print(f"\nError generating responses: {e}")

        if args.backend == "hf" and any(
            name in args.model for name in ["gemma", "llama", "mistral"]
        ):
            print("\nThis appears to be an authentication issue with a gated model.")
            print("Try using the LLM tool with Gemini API instead:")
            print(
                f"python demo.py --backend llm --model gemini-1.5-pro-latest --person {args.person}"
                + (f' --topic "{args.topic}"' if args.topic else "")
                + (f' --message "{args.message}"' if args.message else "")
            )
        # Ollama is now handled through the llm backend
        elif args.backend == "llm":
            if "gemini" in args.model.lower() or "gemma" in args.model.lower():
                print(
                    "\nMake sure you have the GEMINI_API_KEY environment variable set:"
                )
                print("export GEMINI_API_KEY=your_api_key")
                print("\nAnd make sure llm-gemini is installed:")
                print("llm install llm-gemini")
            elif "mlx" in args.model.lower():
                print("\nMake sure llm-mlx is installed:")
                print("llm install llm-mlx")
            elif "ollama" in args.model.lower():
                print("\nMake sure Ollama is installed and running:")
                print("1. Install from https://ollama.ai/")
                print("2. Start Ollama with: ollama serve")
                print("3. Install the llm-ollama plugin: llm install llm-ollama")
                model_name = args.model
                if "ollama:" in model_name.lower():
                    model_name = model_name.replace("Ollama: ", "")
                elif "/" in model_name:
                    model_name = model_name.split("/")[1]
                print(f"4. Pull the model: ollama pull {model_name}")
            else:
                print("\nMake sure Simon Willison's LLM tool is installed:")
                print("pip install llm")


# If running as a script
if __name__ == "__main__":
    main()