File size: 2,755 Bytes
cbdc6bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
from huggingface_hub import InferenceClient

class ChatBot:
    def __init__(self, model_name="HelpingAI/HELVETE-3B"):
        """Initialize the chatbot with the specified model."""
        # Get API token from environment variable
        self.api_key = os.environ.get("HF_TOKEN")
        if not self.api_key:
            print("Error: HF_TOKEN environment variable not set")
            print("Please set your Hugging Face token with: export HF_TOKEN=your_token_here")
            sys.exit(1)
        
        self.client = InferenceClient(api_key=self.api_key)
        self.model = model_name
        self.conversation_history = []
    
    def add_message(self, role, content):
        """Add a message to the conversation history."""
        self.conversation_history.append({"role": role, "content": content})
    
    def get_response(self, user_message):
        """Get a response from the AI model."""
        # Add user message to history
        self.add_message("user", user_message)
        
        try:
            # Get completion from the model
            completion = self.client.chat.completions.create(
                model=self.model,
                messages=self.conversation_history,
            )
            
            # Extract the assistant's response
            assistant_response = completion.choices[0].message.content
            
            # Add assistant response to history
            self.add_message("assistant", assistant_response)
            
            return assistant_response
            
        except Exception as e:
            return f"Error: {str(e)}"
    
    def chat_loop(self):
        """Run an interactive chat loop."""
        print("=" * 50)
        print("πŸ€– HELVETE-3B ChatBot")
        print("Type 'quit', 'exit', or 'bye' to end the conversation")
        print("=" * 50)
        
        while True:
            try:
                user_input = input("\nYou: ").strip()
                
                if user_input.lower() in ['quit', 'exit', 'bye']:
                    print("Goodbye! πŸ‘‹")
                    break
                
                if not user_input:
                    continue
                
                print("AI: ", end="", flush=True)
                response = self.get_response(user_input)
                print(response)
                
            except KeyboardInterrupt:
                print("\n\nConversation interrupted. Goodbye! πŸ‘‹")
                break
            except Exception as e:
                print(f"\nAn error occurred: {e}")

def main():
    """Main function to run the chatbot."""
    # Create and run the chatbot
    bot = ChatBot()
    bot.chat_loop()

if __name__ == "__main__":
    main()