mirascope icon indicating copy to clipboard operation
mirascope copied to clipboard

o1 style COT creations

Open KTS-o7 opened this issue 4 months ago • 4 comments

Description

Can we create some kind of interface which simulates COT kind of thinking. Currently for Groq we have g1. Can we provide something which can help people to build quick prototypes and understand how COT improves their usecase.

Something like this but more robust stuff

from mirascope.core import openai, prompt_template
from openai.types.chat import ChatCompletionMessageParam
from pydantic import BaseModel
import json

class Chatbot(BaseModel):
    history: list[ChatCompletionMessageParam] = []

    @openai.call(model="gpt-4o-mini", stream=True)
    @prompt_template(
        """
        SYSTEM: You are a helpful assistant.
        MESSAGES: {self.history}
        USER: {question}
        """
    )
    def _call(self, question: str): ...

    def generate_cot_response(self, user_query):
        # This is a placeholder for the actual logic that generates the COT response
        # In a real implementation, this would call a model or some other logic to generate the steps
        steps = [
            ("Step 1: Identify the task", "The task is to count the number of 'R's in the word 'strawberry'.", 1.2),
            ("Step 2: Break down the task", "Split the word into individual characters and count the 'R's.", 1.5),
            ("Step 3: Execute the task", "The word 'strawberry' has 3 'R's.", 0.8),
            ("Final Answer", "There are 3 'R's in the word 'strawberry'.", None)
        ]
        total_thinking_time = sum(thinking_time for _, _, thinking_time in steps if thinking_time is not None)
        return steps, total_thinking_time

    def display_cot_response(self, steps, total_thinking_time):
        for i, (title, content, thinking_time) in enumerate(steps):
            # Ensure content is a string
            if not isinstance(content, str):
                content = json.dumps(content)
            if title.startswith("Final Answer"):
                print(f"### {title}")
                if '```' in content:
                    parts = content.split('```')
                    for index, part in enumerate(parts):
                        if index % 2 == 0:
                            print(part)
                        else:
                            if '\n' in part:
                                lang_line, code = part.split('\n', 1)
                                lang = lang_line.strip()
                            else:
                                lang = ''
                                code = part
                            print(f"```{lang}\n{code}\n```")
                else:
                    print(content.replace('\n', '<br>'))
            else:
                print(f"{title}:")
                print(content.replace('\n', '<br>'))
        
        if total_thinking_time is not None:
            print(f"**Total thinking time: {total_thinking_time:.2f} seconds**")

    def run(self):
        while True:
            question = input("(User): ")
            if question in ["quit", "exit"]:
                print("(Assistant): Have a great day!")
                break
            
            # Generate COT response
            steps, total_thinking_time = self.generate_cot_response(question)
            self.display_cot_response(steps, total_thinking_time)
            
            # Call the assistant and stream the response
            stream = self._call(question)
            print("(Assistant): ", end="", flush=True)
            for chunk, _ in stream:
                print(chunk.content, end="", flush=True)
            print("")
            if stream.user_message_param:
                self.history.append(stream.user_message_param)
            self.history.append(stream.message_param)

# Run the chatbot
Chatbot().run()

Ps : Would be great if you guys provide some hint on how i can begin on this IF its worthwhile. Would be nice if it counts for Hacktoberfest : )

KTS-o7 avatar Oct 01 '24 16:10 KTS-o7