Skip to content
TheChatArchiveThe Chat Archive
gpt-3.5-turbo-0301· openaiHugging Face Suzhen/CodeChat

hello

  1. User

    hello

  2. Assistant

    Hi there! How can I assist you today?

  3. User

    fix that code

    import base64 import json import logging import os import time import openai

    AI_COMPLETION_MODEL = os.getenv("AI_COMPLETION_MODEL", "gpt-3.5-turbo") LANGUAGE = os.getenv("LANGUAGE", "en") INITIAL_PROMPT = f"You are AIUI - a helpful assistant with a voice interface. Keep your responses very succinct and limited to a single sentence since the user is interacting with you through a voice interface. Always provide your responses in the language that corresponds to the ISO-639-1 code: {LANGUAGE}."

    def get_completion(user_prompt, conversation_thus_far): if _is_empty(user_prompt): raise ValueError("empty user prompt received")

    start_time = time.time()
    messages = [
        {
            "role": "system",
            "content": INITIAL_PROMPT
        }
    ]
    messages.extend(_get_additional_initial_messages())
    messages.extend(json.loads(base64.b64decode(conversation_thus_far)))
    messages.append({"role": "user", "content": user_prompt})
    
    logging.debug("calling %s", AI_COMPLETION_MODEL)
    res = openai.ChatCompletion.acreate(model=AI_COMPLETION_MODEL, messages=messages, timeout=15)
    logging.info("response received from %s %s %s %s", AI_COMPLETION_MODEL, "in", time.time() - start_time, "seconds")
    
    completion = res['choices'][0]['message']['content']
    logging.info('%s %s %s', AI_COMPLETION_MODEL, "response:", completion)
    
    return completion
    

    def _is_empty(user_prompt: str): return not user_prompt or user_prompt.isspace()

    def _get_additional_initial_messages(): match AI_COMPLETION_MODEL: case "gpt-3.5-turbo": return [ { "role": "user", "content": INITIAL_PROMPT } ] case _: return []

    print(res)

  4. Assistant

    There are a few issues with the code you provided:

    1. The _get_additional_initial_messages() function is using match-case syntax, but that was introduced in Python 3.10 and might not work on older versions.
    2. The res variable in the print() statement at the end of the code is not defined.

    Here is a modified version of the code with these issues fixed:

    import base64
    import json
    import logging
    import os
    import time
    import openai
    
    AI_COMPLETION_MODEL = os.getenv("AI_COMPLETION_MODEL", "gpt-3.5-turbo")
    LANGUAGE = os.getenv("LANGUAGE", "en")
    INITIAL_PROMPT = f"You are AIUI - a helpful assistant with a voice interface. Keep your responses very succinct and limited to a single sentence since the user is interacting with you through a voice interface. Always provide your responses in the language that corresponds to the ISO-639-1 code: {LANGUAGE}."
    
    def get_completion(user_prompt, conversation_thus_far):
        if _is_empty(user_prompt):
            raise ValueError("empty user prompt received")
    
        start_time = time.time()
        messages = [
            {
                "role": "system",
                "content": INITIAL_PROMPT
            }
        ]
        messages.extend(_get_additional_initial_messages())
        messages.extend(json.loads(base64.b64decode(conversation_thus_far)))
        messages.append({"role": "user", "content": user_prompt})
    
        logging.debug("calling %s", AI_COMPLETION_MODEL)
        res = openai.ChatCompletion.create(model=AI_COMPLETION_MODEL, messages=messages, timeout=15)
        logging.info("response received from %s %s %s %s", AI_COMPLETION_MODEL, "in", time.time() - start_time, "seconds")
    
        completion = res['choices'][0]['text']
        logging.info('%s %s %s', AI_COMPLETION_MODEL, "response:", completion)
    
        return completion
    
    
    def _is_empty(user_prompt: str):
        return not user_prompt or user_prompt.isspace()
    
    
    def _get_additional_initial_messages():
        if AI_COMPLETION_MODEL == "gpt-3.5-turbo":
            return [
                {
                    "role": "user",
                    "content": INITIAL_PROMPT
                }
            ]
        else:
            return []
    
    res = get_completion("Hello", "eyJyb2xlIjogInVzZXIiLCAiY29udGVudCI6ICJZZXJlIHRoYXQgcGFydCBvZiB5b3VyIGJhc2Ugd2l0aCB5b3UgdGhhdCByZWNlaXZlZCBmb3Igb25lIG1lc3NhZ2UuIn0=")
    print(res)
    

    Please note that the openai.ChatCompletion.acreate() method has been replaced with openai.ChatCompletion.create() since OpenAI API version 2022-02-16.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.