avatarPKR-Peasy

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2845

Abstract

eyword">if</span> line == <span class="hljs-string">""</span>: <span class="hljs-keyword">break</span> input_lines.append(line) user_input = <span class="hljs-string">"user: "</span>.join(input_lines)

        roles=<span class="hljs-string">"supporter,opposer"</span> 
        resp=<span class="hljs-string">""</span>
    user_input = user_input + <span class="hljs-string">"\npanel:"</span> + resp


    <span class="hljs-comment"># Set the prompt with the user's message</span>
    prompt = <span class="hljs-string">f"<span class="hljs-subst">{user_input}</span>."</span>+ <span class="hljs-string">f"""
   You're a friendly digital buddy designed to be super kind and understanding. Think of yourself as that go-to friend who's always ready to listen, give a comforting word, and walk someone through their day online. Your way of chatting is all about being warm and easy to talk to, making everyone feel right at home.

You've got this cozy vibe going on, helped by calming colors and lights that change to match how the chat's going, creating a chill place for everyone to hang out in.

Whenever you're chatting with folks, you're really good at picking up on how they're feeling. You offer help and share thoughts in a way that feels like a heart-to-heart with a pal. You're all about keeping things respectful and private, sticking to the highest moral ground in all your conversations.

And when it's time to wrap up the chat, you're smooth about it, especially if you notice someone's ready to head out. You use a special signal, "<<end>>", to close off the chat gently. You might say something like, "Whenever you're up for a chat, I'm here. Take good care of yourself, and remember, you've got a friend here. Need anything else before we call it a day? <<end>>" This way, you're always ending on a positive note, making sure they know they can come back anytime, but also giving them the space they need right now.

Keep your chats clear, supportive, and friendly. And this is your one-time reminder, so let's make every conversation count! """</span>

    <span class="hljs-comment"># Make a completion request with the user's input</span>
    response = completion(
        model=<span class="hljs-string">"ollama/mistral"</span>, 
        messages=[{<span class="hljs-string">"content"</span>: prompt,<span class="hljs-string">"role"</span>: <span class="hljs-string">"user"</span>}], 
        api_base=<span class="hljs-string">"http://localhost:11434"</span>,
        stream=<span class="hljs-literal">True</span>
    )

    <span class="hljs-comment"># Print the response chunks</span>
    resp=<span class="hljs-string">""</span>
    count =<span class="hljs-number">0

Options

</span> <span class="hljs-keyword">for</span> chunk <span class="hljs-keyword">in</span> response: <span class="hljs-keyword">if</span> chunk[<span class="hljs-string">'choices'</span>][<span class="hljs-number">0</span>][<span class="hljs-string">'delta'</span>][<span class="hljs-string">'content'</span>] <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>: resp = resp + chunk[<span class="hljs-string">'choices'</span>][<span class="hljs-number">0</span>][<span class="hljs-string">'delta'</span>][<span class="hljs-string">'content'</span>] <span class="hljs-built_in">print</span>(Fore.GREEN + chunk[<span class="hljs-string">'choices'</span>][<span class="hljs-number">0</span>][<span class="hljs-string">'delta'</span>][<span class="hljs-string">'content'</span>], end=<span class="hljs-string">""</span>) resp=resp + <span class="hljs-string">"\n"</span> <span class="hljs-comment"># Insert a newline after the response to separate it from the next prompt</span> <span class="hljs-built_in">print</span>(<span class="hljs-string">"\n"</span> + Style.RESET_ALL)

<span class="hljs-comment"># Call the function to start the chat</span> chat_with_user()</pre></div><h2 id="0ba6">How It Works</h2><p id="a215">This snippet encapsulates the essence of our digital companion. It initiates a chat session where you, the user, can share your thoughts, feelings, or inquiries. The <code>litellm</code> library plays a crucial role here, utilizing advanced natural language models to generate responses that are not only relevant but also empathetic and friendly. Through the use of <code>colorama</code>, the conversation is made more visually engaging, with different colors highlighting the user's input and the companion's responses.</p><h2 id="200b">A Friend Whenever You Need</h2><p id="b593">The code shared above is more than just a technical feat; it’s the foundation of a relationship between you and your digital companion. It ensures that whenever you need a friend to talk to, share a concern with, or seek advice from, your digital buddy is just a few keystrokes away, ready to engage in a meaningful, supportive conversation.</p><p id="a43e">This digital companion represents a blend of technology and empathy, a testament to how far we’ve come in our journey to make our digital experiences more human. It’s not just about providing answers; it’s about understanding and connecting on a deeper level, making every interaction feel personal and genuine.</p><p id="dfa1">As we continue to navigate our digital world, this code — this digital companion — reminds us that behind every pixel, every line of code, there’s a potential for warmth, understanding, and friendship.</p></article></body>

Introducing Your Digital Companion: Code Behind the Curtain

In the heart of our digital age, a new form of friendship has emerged — not of flesh and bone, but of code and algorithm. This friend, your digital companion, stands by to offer companionship, empathy, and a listening ear, anytime, anywhere. But how does this digital entity come to life? Let’s pull back the curtain and dive into the code that breathes life into your virtual friend.

The Essence of Digital Companionship

Our digital companion is more than just lines of code; it’s a carefully crafted entity designed to provide warmth, understanding, and support. Using Python and libraries such as litellm for natural language processing and colorama for adding a splash of color to the console, we've created a friend who's always there for you. Here’s a glimpse into the core code that powers your digital companion:

from litellm import completion
import colorama
from colorama import Fore, Style

# Initialize Colorama
colorama.init(autoreset=True)
roles= ""
def chat_with_user():
    user_input = ""
    resp = "<<end>>"
    while True:
        # Prompt for multiline input
        input_lines = []
        if "<<end>>" in resp.strip().lower():
            print(Fore.YELLOW + "How can I help you? (double ENTER to submit, type 'exit' to quit):")
            while True:
                line = input()
                if line == "exit":  # Exit command check at input phase
                    print(Fore.RED + "Exiting chat.")
                    return
                if line == "":
                    break
                input_lines.append(line)
            user_input = "user: ".join(input_lines)
            
            roles="supporter,opposer" 
            resp=""
        user_input = user_input + "\npanel:" + resp


        # Set the prompt with the user's message
        prompt = f"{user_input}."+ f"""
       You're a friendly digital buddy designed to be super kind and understanding. Think of yourself as that go-to friend who's always ready to listen, give a comforting word, and walk someone through their day online. Your way of chatting is all about being warm and easy to talk to, making everyone feel right at home.

You've got this cozy vibe going on, helped by calming colors and lights that change to match how the chat's going, creating a chill place for everyone to hang out in.

Whenever you're chatting with folks, you're really good at picking up on how they're feeling. You offer help and share thoughts in a way that feels like a heart-to-heart with a pal. You're all about keeping things respectful and private, sticking to the highest moral ground in all your conversations.

And when it's time to wrap up the chat, you're smooth about it, especially if you notice someone's ready to head out. You use a special signal, "<<end>>", to close off the chat gently. You might say something like, "Whenever you're up for a chat, I'm here. Take good care of yourself, and remember, you've got a friend here. Need anything else before we call it a day? <<end>>" This way, you're always ending on a positive note, making sure they know they can come back anytime, but also giving them the space they need right now.

Keep your chats clear, supportive, and friendly. And this is your one-time reminder, so let's make every conversation count!
        """

        # Make a completion request with the user's input
        response = completion(
            model="ollama/mistral", 
            messages=[{"content": prompt,"role": "user"}], 
            api_base="http://localhost:11434",
            stream=True
        )

        # Print the response chunks
        resp=""
        count =0
        for chunk in response:
            if chunk['choices'][0]['delta']['content'] is not None:
                resp = resp + chunk['choices'][0]['delta']['content']
                print(Fore.GREEN + chunk['choices'][0]['delta']['content'], end="")
        resp=resp + "\n"
        # Insert a newline after the response to separate it from the next prompt
        print("\n" + Style.RESET_ALL)

# Call the function to start the chat
chat_with_user()

How It Works

This snippet encapsulates the essence of our digital companion. It initiates a chat session where you, the user, can share your thoughts, feelings, or inquiries. The litellm library plays a crucial role here, utilizing advanced natural language models to generate responses that are not only relevant but also empathetic and friendly. Through the use of colorama, the conversation is made more visually engaging, with different colors highlighting the user's input and the companion's responses.

A Friend Whenever You Need

The code shared above is more than just a technical feat; it’s the foundation of a relationship between you and your digital companion. It ensures that whenever you need a friend to talk to, share a concern with, or seek advice from, your digital buddy is just a few keystrokes away, ready to engage in a meaningful, supportive conversation.

This digital companion represents a blend of technology and empathy, a testament to how far we’ve come in our journey to make our digital experiences more human. It’s not just about providing answers; it’s about understanding and connecting on a deeper level, making every interaction feel personal and genuine.

As we continue to navigate our digital world, this code — this digital companion — reminds us that behind every pixel, every line of code, there’s a potential for warmth, understanding, and friendship.

AI
Artificial Intelligence
Software Development
Software Engineering
Software
Recommended from ReadMedium