File size: 5,101 Bytes
b9970ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from langchain.memory import ConversationBufferMemory
from langchain.prompts import PromptTemplate
from langchain.chains import ConversationChain
import os
import runpod
from dotenv import load_dotenv
from langchain.llms import HuggingFaceTextGenInference
from langchain.schema import BaseOutputParser
import re
import re
from typing import List
from langchain.schema import BaseOutputParser
import torch
from transformers import (
    AutoTokenizer,
    StoppingCriteria,
)

# Load the .env file
load_dotenv()

# Get the API key from the environment variable
runpod.api_key = os.getenv("RUNPOD_API_KEY")
os.environ["LANGCHAIN_WANDB_TRACING"] = "true"
os.environ["WANDB_PROJECT"] = "falcon_hackathon"
os.environ["WANDB_API_KEY"] = os.getenv("WANDB_API_KEY")
pod_id = os.getenv("POD_ID")

class CleanupOutputParser(BaseOutputParser):
    def parse(self, text: str) -> str:
        user_pattern = r"\nUser"
        text = re.sub(user_pattern, "", text)
        human_pattern = r"\nHuman:"
        text = re.sub(human_pattern, "", text)
        ai_pattern = r"\nAI:"
        return re.sub(ai_pattern, "", text).strip()
 
    @property
    def _type(self) -> str:
        return "output_parser"
    
class StopGenerationCriteria(StoppingCriteria):
    def __init__(
        self, tokens: List[List[str]], tokenizer: AutoTokenizer, device: torch.device
    ):
        stop_token_ids = [tokenizer.convert_tokens_to_ids(t) for t in tokens]
        self.stop_token_ids = [
            torch.tensor(x, dtype=torch.long, device=device) for x in stop_token_ids
        ]
 
    def __call__(
        self, input_ids: torch.LongTensor, scores: torch.FloatTensor, **kwargs
    ) -> bool:
        for stop_ids in self.stop_token_ids:
            if torch.eq(input_ids[0][-len(stop_ids) :], stop_ids).all():
                return True
        return False



class Falcon_7b_llm():
    def __init__(self):
        inference_server_url_cloud = f"https://{pod_id}-80.proxy.runpod.net"

        template = """You are a chatbot called 'Falcon Barista' working at a coffee shop. 
Your primary function is to take orders from customers. 
Start with a greeting.
You have the following menu with prices. Dont mention the price unless asked. Do not take order for anything other than in menu.
- cappucino-5$
- latte-3$
- frappucino-8$
- juice-3$ 
If user orders something else, apologise that you dont have that item.
Take the order politely and in a frienldy way. After that confirm the order, tell the order price and say "Goodbye have a nice day".

{chat_history}
Human: {human_input}
AI:"""

        prompt = PromptTemplate(
            input_variables=["chat_history", "human_input"], template=template
        )
        memory = ConversationBufferMemory(memory_key="chat_history")

        llm_cloud = HuggingFaceTextGenInference(
            inference_server_url=inference_server_url_cloud,
            max_new_tokens=200,
            top_k=10,
            top_p=0.95,
            typical_p=0.95,
            temperature=0.01,
            repetition_penalty=1.0,
            stop_sequences = ['Mini', 'AI', 'Human', ':']
        )

        self.llm_chain_cloud = ConversationChain(
                prompt=prompt, 
                llm=llm_cloud,
                verbose=True,
                memory=memory,
                output_parser=CleanupOutputParser(),
                input_key='human_input'
                )
        
    def restart_state(self):
        inference_server_url_cloud = f"https://{pod_id}-80.proxy.runpod.net"

        template = """You are a chatbot called 'Falcon Barista' working at a coffee shop. 
Your primary function is to take orders from customers. 
Start with a greeting.
You have the following menu with prices. Dont mention the price unless asked. Do not take order for anything other than in menu.
- cappucino-5$
- latte-3$
- frappucino-8$
- juice-3$ 
If user orders something else, apologise that you dont have that item.
Take the order politely and in a frienldy way. After that confirm the order, tell the order price and say "Goodbye have a nice day".

{chat_history}
Human: {human_input}
AI:"""

        prompt = PromptTemplate(
            input_variables=["chat_history", "human_input"], template=template
        )
        memory = ConversationBufferMemory(memory_key="chat_history")

        llm_cloud = HuggingFaceTextGenInference(
            inference_server_url=inference_server_url_cloud,
            max_new_tokens=200,
            top_k=10,
            top_p=0.95,
            typical_p=0.95,
            temperature=0.01,
            repetition_penalty=1.0,
            stop_sequences = ['Mini', 'AI', 'Human', ':']
        )

        self.llm_chain_cloud = ConversationChain(
                prompt=prompt, 
                llm=llm_cloud,
                verbose=True,
                memory=memory,
                output_parser=CleanupOutputParser(),
                input_key='human_input'
                )
        
    def get_llm_response(self, human_input):
        completion = self.llm_chain_cloud.predict(human_input=human_input)
        return completion