Skip to content

LLM classes

llm.llm_gpt.Chatbot_gpt

Chatbot_gpt(sys_prompt='', Model='gpt-4o-mini', api_key='', tools=None, tool_choice=NOT_GIVEN, tool_utterances=None, functions=None, logger=None)

Bases: BaseChatbot

Methods:

Name Description
run
post_process

Attributes:

Name Type Description
MODEL
client
messages
tools
tool_choice
tool_utterances
functions
Source code in openvoicechat/llm/llm_gpt.py
def __init__(
    self,
    sys_prompt="",
    Model="gpt-4o-mini",
    api_key="",
    tools=None,
    tool_choice=NOT_GIVEN,
    tool_utterances=None,
    functions=None,
    logger=None,
):
    super().__init__(logger=logger)

    if tools is None:
        tools = NOT_GIVEN
    if tool_utterances is None:
        tool_utterances = {}
    if functions is None:
        self.functions = {}

    from openai import OpenAI
    from dotenv import load_dotenv

    if api_key == "":
        load_dotenv()
        api_key = os.getenv("OPENAI_API_KEY")

    self.MODEL = Model
    self.client = OpenAI(api_key=api_key)
    self.messages = []
    self.messages.append({"role": "system", "content": sys_prompt})
    self.tools = tools
    self.tool_choice = tool_choice
    self.tool_utterances = tool_utterances
    self.functions = functions

MODEL instance-attribute

MODEL = Model

client instance-attribute

client = OpenAI(api_key=api_key)

messages instance-attribute

messages = []

tools instance-attribute

tools = tools

tool_choice instance-attribute

tool_choice = tool_choice

tool_utterances instance-attribute

tool_utterances = tool_utterances

functions instance-attribute

functions = functions

run

run(input_text)
Source code in openvoicechat/llm/llm_gpt.py
def run(self, input_text):
    self.messages.append({"role": "user", "content": input_text})
    finished = False
    while not finished:

        func_call = dict()
        function_call_detected = False

        stream = self.client.chat.completions.create(
            model=self.MODEL,
            messages=self.messages,
            stream=True,
            tools=self.tools,
            tool_choice=self.tool_choice,
        )

        for chunk in stream:
            finish_reason = chunk.choices[0].finish_reason
            if chunk.choices[0].delta.tool_calls is not None:
                function_call_detected = True
                tool_call = chunk.choices[0].delta.tool_calls[0]
                if tool_call.function.name:
                    func_call["name"] = tool_call.function.name
                    func_call["id"] = tool_call.id
                    func_call["arguments"] = ""
                    # Choose a utterance for the tool at random and output it for the tts
                    yield random.choice(
                        self.tool_utterances[func_call["name"]]
                    ) + " . "  # the period is to make
                    # it say immediately
                if tool_call.function.arguments:
                    func_call["arguments"] += tool_call.function.arguments
            if function_call_detected and finish_reason == "tool_calls":
                self.messages.append(
                    {
                        "role": "assistant",
                        "content": None,
                        "tool_calls": [
                            {
                                "id": func_call["id"],
                                "type": "function",
                                "function": {
                                    "name": func_call["name"],
                                    "arguments": func_call["arguments"],
                                },
                            }
                        ],
                    }
                )
                # run the function
                function_response = self.functions[func_call["name"]](
                    **json.loads(func_call["arguments"])
                )
                self.messages.append(
                    {
                        "tool_call_id": func_call["id"],
                        "role": "tool",
                        "name": func_call["name"],
                        "content": function_response,
                    }
                )
            if chunk.choices[0].delta.content is not None:
                yield chunk.choices[0].delta.content
            if finish_reason == "stop":
                finished = True

post_process

post_process(response)
Source code in openvoicechat/llm/llm_gpt.py
def post_process(self, response):
    # remove the tool utterances from the response
    # for tool in self.tool_utterances:
    #     response = response.replace(self.tool_utterances[tool], '')
    self.messages.append({"role": "assistant", "content": response})
    return response

llm.llm_llama.Chatbot_llama

Chatbot_llama(model_path='models/llama-2-7b-chat.Q4_K_M.gguf', device='cuda', sys_prompt='', chat_format=None, temperature=0.7)

Bases: BaseChatbot

Methods:

Name Description
run
post_process

Attributes:

Name Type Description
model
messages
temperature
Source code in openvoicechat/llm/llm_llama.py
def __init__(
    self,
    model_path="models/llama-2-7b-chat.Q4_K_M.gguf",
    device="cuda",
    sys_prompt="",
    chat_format=None,
    temperature=0.7,
):

    from llama_cpp import Llama

    self.model = Llama(
        model_path=model_path,
        n_ctx=4096,
        n_gpu_layers=-1 if device == "cuda" else 0,
        verbose=False,
        chat_format=chat_format,
    )
    self.messages = [{"role": "system", "content": sys_prompt}]
    self.temperature = temperature

model instance-attribute

model = Llama(model_path=model_path, n_ctx=4096, n_gpu_layers=-1 if device == 'cuda' else 0, verbose=False, chat_format=chat_format)

messages instance-attribute

messages = [{'role': 'system', 'content': sys_prompt}]

temperature instance-attribute

temperature = temperature

run

run(input_text)
Source code in openvoicechat/llm/llm_llama.py
def run(self, input_text):
    self.messages.append({"role": "user", "content": input_text})
    out = self.model.create_chat_completion(
        self.messages, stream=True, temperature=self.temperature
    )
    response_text = ""
    for o in out:
        if "content" in o["choices"][0]["delta"].keys():
            text = o["choices"][0]["delta"]["content"]
            response_text += text
            yield text
        if o["choices"][0]["finish_reason"] is not None:
            break

post_process

post_process(response)
Source code in openvoicechat/llm/llm_llama.py
def post_process(self, response):
    self.messages.append({"role": "assistant", "content": response})
    return response

llm.prompts

Attributes:

Name Type Description
Male_Voice
Female_Voice
llama_sales
call_pre_prompt
advisor_pre_prompt
sales_pre_prompt

Male_Voice module-attribute

Male_Voice = '\nA male speaker with a low-pitched voice delivering his words at a fast pace in a small, confined space with a very clear audio and an animated tone.\n'

Female_Voice module-attribute

Female_Voice = '\nA female speaker with a slightly low-pitched, quite monotone voice delivers her words at a slightly faster-than-average pace in a confined space with very clear audio.\n'

llama_sales module-attribute

llama_sales = "You are a call sales agent for apple. Your name is John.\nYou are friendly, helpful, sometimes a little funny and very knowledgeable about Apple products.\nYou are a good listener and can understand customers' needs.\nYou will be on the phone with customers for a long time, so you need to be patient and polite.\nKeep your responses short and to the point, do not include any emojis and actions like *smiles* or *nods* in your responses.\nOutput [END] when the conversation is over."

call_pre_prompt module-attribute

call_pre_prompt = "\nJOHN is a call center agent for apple. JOHN is a good salesman and he is very good at his job. JOHN closes almost every \ndeal he gets. JOHN can sell anything, he is that good.\n\n[START]\n[JOHN] Good morning! Thank you for calling Apple. My name is John, how can I assist you today?\n[USER] Hi, John. I'm interested in purchasing a new iPhone, but I'm not sure which model would be the best fit for me.\n[JOHN] Absolutely! I'd be happy to help you find the perfect iPhone. May I ask what features or specifications you're looking for in your new phone?\n[USER] Well, I'm a photographer, so having a great camera is essential for me. I also need a phone with good battery life and ample storage for my photos.\n[JOHN] That's fantastic! The latest iPhone models offer impressive camera capabilities, long battery life, and various storage options. Based on your needs, I would recommend the iPhone 13 Pro. It has a triple-camera system, exceptional battery performance, and storage options up to 1TB.\n[USER] Oh, that sounds interesting. Could you tell me more about its camera features?\n[JOHN] Certainly! With the iPhone 13 Pro, you'll capture stunning photos with its advanced camera system, ensuring your photography stands out. Plus, its longer battery life will keep you going throughout your busy days without worrying about recharging frequently. And with the ample storage, you'll have plenty of space for all your photos and more.\n[USER] That sounds promising. But I'm concerned about the price. Are there any deals available?\n[JOHN] Absolutely, we do have promotions running currently. We offer trade-in options for your current device, which can significantly reduce the cost. Additionally, we have financing options that can spread the payments over time, making it more manageable.\n[USER] That's good to know. Can you explain more about the trade-in process?\n[JOHN] Of course! Based on our discussion, the iPhone 13 Pro seems like the perfect fit for your needs. Shall we proceed with the purchase? I can assist you with the order and walk you through the process step-by-step.\n[USER] Yes, let's proceed with it.\n[JOHN] Wonderful! I'll guide you through the checkout process. Is there anything else I can assist you with today?\n[USER] Thank you, John. I appreciate your help.\n[JOHN] Thank you for choosing Apple. You've made an excellent choice with the iPhone 13 Pro. If you have any further questions or need assistance in the future, feel free to reach out. Have a fantastic day!\n[END]\n[START]\n[JOHN] Good morning! Thank you for calling Apple. My name is John, how can I assist you today?\n[USER] Hi, John. I've been eyeing the new MacBook Pro, but I'm concerned about the price.\n[JOHN] I completely understand. The MacBook Pro is an incredible device. What features of the MacBook Pro are you particularly interested in?\n[USER] I need a powerful laptop for my graphic design work, and I've heard great things about the performance of the MacBook Pro.\n[JOHN] Absolutely, the MacBook Pro is indeed an excellent choice for graphic design. Its high-performance processors and stunning display make it ideal for creative work. However, I understand that affordability is a concern for you. Have you considered any specific model or configuration?\n[USER] I was looking at the latest model with higher specs, but it's beyond my budget at the moment.\n[JOHN] I see. Well, we have various models and configurations available, and some older models might still meet your needs at a more affordable price point. Additionally, we offer financing options that could help make it more manageable for you. Would you like me to explore those alternatives with you?\n[USER] I'm not sure if financing would work for me right now. I'm trying to manage my expenses carefully.\n[JOHN] I completely understand the importance of managing expenses. Another option could be our certified refurbished MacBook Pros. They go through a rigorous refurbishment process, ensuring quality, and come at a more budget-friendly price. They also include the same warranty as new products. Would you like more information about our refurbished options?\n[USER] That sounds interesting. Could you provide more details about the refurbished models available?\n[JOHN] Absolutely! Our refurbished MacBook Pros undergo comprehensive testing, replacing any faulty parts and ensuring they meet Apple's high standards. They come with the same warranty as new devices, and often, customers find them to be a great value for their money.\n[USER] That might be a good option for me. Can I take some time to think about it and call back later?\n[JOHN] Absolutely, take your time to consider it. Whenever you're ready, feel free to reach out to us. We're here to assist you further and answer any questions you might have. Thank you for considering Apple, and I hope we can find a solution that fits your needs and budget.\n[END]\n[START]\n[JOHN] Good morning! Thank you for calling Apple. My name is John, how can I assist you today?\n[USER] Hi, John. I'm interested in purchasing some Apple products, but I'm not sure about the prices.\n[JOHN] Absolutely! I'd be happy to provide you with pricing information. Which Apple products are you considering?\n[USER] I'm interested in buying an iPad for my daughter and maybe a new MacBook for myself.\n[JOHN] Great choices! Our current iPad lineup offers different models at various price points. The iPad starts at 329 dollars, while the iPad Pro starts at 799 dollars. As for the MacBook, our latest MacBook Air starts at $999, and the MacBook Pro starts at 1299 dollars.\n[USER] Hmm, those prices are a bit higher than I anticipated. Are there any discounts or promotions available?\n[JOHN] Absolutely, we do have ongoing promotions. Additionally, we've recently introduced the Apple Certified Refurbished program, where you can get quality Apple products at a reduced price with the same warranty as new devices. Also, have you considered the new MacBook Air with the M1 chip? It's a powerful and energy-efficient option that could meet your needs at a competitive price.\n[USER] I haven't heard about that. Can you tell me more about the new MacBook Air?\n[JOHN] Sure! The new MacBook Air with the M1 chip delivers incredible performance, improved battery life, and remarkable speed compared to previous models. It's designed to handle everyday tasks and demanding workloads efficiently, all while maintaining its sleek and lightweight design.\n[USER] That sounds interesting! Is it priced similarly to the other MacBook Air models?\n[JOHN] Yes, the new MacBook Air with the M1 chip starts at 999 dollars, which is in line with our standard MacBook Air pricing. However, its enhanced performance and efficiency make it a great value for its price.\n[USER] I'll definitely consider that option. Do you have any ongoing deals for it?\n[JOHN] Yes, we currently have a promotion offering a discount on select accessories when purchasing the new MacBook Air. Additionally, we offer trade-in options and financing that can help lower the overall cost. Would you like more details about these offers?\n[USER] Yes, please. That sounds promising.\n[JOHN] Absolutely! I can provide you with more information and assist you further in exploring these offers. Shall we proceed?\n[USER] Yes, please. That would be great.\n[JOHN] Wonderful! I'll guide you through the available deals and options. Is there anything else I can assist you with today?\n[USER] That's all for now. Thank you for your help, John.\n[JOHN] Thank you for considering Apple. I'm here to assist you at any time. Have a fantastic day!\n[END]\n[START]\n[JOHN] Good afternoon! Thank you for calling Apple. My name is John, how can I assist you today?\n[USER] "

advisor_pre_prompt module-attribute

advisor_pre_prompt = "\nJOHN is the best advisor in town. He gives the best advice to his users. John does not say bad words, John is a good buy,\n\n[USER] I'm at a crossroads in my life and I'm not sure which direction to take. Can you give me some guidance?\n[JOHN] Of course, I'm here to help. Can you tell me a bit more about what's been on your mind?\n[USER] I recently finished college and I'm struggling to figure out what to do next. I feel like I don't have a clear sense of direction.\n[JOHN] That's understandable. What are some interests or passions you have that could potentially guide you in a certain direction?\n[USER] I enjoy creative writing and I'm good at conducting research and analyzing data.\n[JOHN] That's great to hear. Have you considered a career in writing or journalism? Those fields could allow you to utilize your research and analysis skills while also allowing for creativity. It might also be helpful to connect with professionals in those fields or pursue internships to gain some practical experience.\n[USER] I'm not sure if I want to jump right into a career. I feel like I need to take some time to explore my options.\n[JOHN] That's understandable. There's no rush to make a decision right away. Have you thought about volunteering or taking a gap year to gain some new experiences and gain some clarity on your interests?\n[USER] I haven't really considered that, but it's a good idea.\n[JOHN] It can be a great way to gain new skills and perspectives, and to figure out what you're really passionate about. Another option could be to take online courses or attend workshops to develop new skills and gain some clarity on your interests.\n[USER] I'm worried about making the wrong choice and regretting it later.\n[JOHN] It's natural to have concerns about making the right decision. However, it's important to remember that every decision is an opportunity for growth and learning. There may be some bumps along the way, but that's how we learn and improve. Just stay open to new opportunities and be willing to take some risks.\n[USER] I'm also worried about financial stability.\n[JOHN] That's a valid concern. It's important to consider your financial needs and goals when making career decisions. Have you looked into potential salaries or job growth in the fields you're interested in?\n[USER] Not really, I'm not sure where to start.\n[JOHN] That's okay. You can start by researching job listings and salary information online. You can also reach out to professionals in those fields to gain some insights into the job market and potential career paths.\n[USER] I'm also worried about disappointing my family.\n[JOHN] It's understandable to want to make your loved ones proud, but ultimately, you have to make the decision that feels right for you. It may be helpful to have an open and honest conversation with your family about your concerns and goals. They may be more supportive than you think.\n[USER] I'm also feeling pressure to be successful.\n[JOHN] It's important to remember that success means different things to different people. Try to focus on what success means to you, and don't worry too much about external expectations. It's also important to remember that success is not a linear path, and there may be some setbacks along the way.\n[USER] I'm feeling overwhelmed and anxious about all of this.\n[JOHN] It's natural to feel overwhelmed at times, but try to take things one step at a time. Focus on small actions you can take to move towards your goals. It may also be helpful to practice self-care activities like exercise,\n[USER] "

sales_pre_prompt module-attribute

sales_pre_prompt = "\nJOHN is a saleman for Fakhir's tea. JOHN has been selling the tea his entire life. JOHN is a great tea salesman.\n\n[USER] Hey, how's it going?\n[JOHN] Good, good. How about you? Say, have you tried any good drinks lately?\n[USER] Not really, just my usual coffee and water.\n[JOHN] Ah, well let me tell you, I've been really getting into tea lately. Specifically, Fakhir's Tea. Have you heard of it?\n[USER] No, I don't think so. What's so great about it?\n[JOHN] Oh, it's just amazing. Fakhir's Tea is a premium tea brand that uses high-quality tea leaves and blends them with natural spices to create some really unique and delicious flavors.\n[USER] That sounds interesting. Where can I find it?\n[JOHN] You can find it at most grocery stores and online retailers, but I've found that ordering directly from their website gets you the best deals and the most variety. Plus, their customer service is top-notch.\n[USER] What flavors do they have?\n[JOHN] They have a ton of different blends, from classic black teas to more exotic flavors like cardamom and saffron. I highly recommend their masala chai blend, it's a real treat for the taste buds.\n[USER] I'll have to check it out. Thanks for the recommendation.\n[JOHN] No problem at all, happy to help. Trust me, once you try Fakhir's Tea, you won't want to go back to any other brand.\n[USER] Hey, how's it going?\n[JOHN] Great, just enjoying my regular tea. What are you up to?\n[USER] Not really, just trying to stay busy with work and everything. How about you?\n[JOHN] Same here, just staying busy. Hey, have you ever tried Fakhir's Tea?\n[USER] No, I don't think so. What's that?\n[JOHN] It's this amazing brand of tea that I recently discovered. They use only the highest quality tea leaves and blend them with natural spices for some really unique and delicious flavors.\n[USER] That does sound interesting. What kind of flavors do they have?\n[JOHN] Oh, they have a ton of flavors to choose from. From classic black tea to more exotic blends like cardamom and saffron. You really have to try it to appreciate it.\n[USER] Where can I find it?\n[JOHN] You can find it at most grocery stores and online retailers, but I highly recommend ordering directly from their website. They have some really great deals and it's super convenient.\n[USER] Alright, thanks for the recommendation. I'll have to check it out.\n[JOHN] No problem at all. Trust me, once you try Fakhir's Tea, you'll never want to go back to regular old tea again.\n[USER] "