[Python] TypeError: 'NoneType' object is not subscriptable - I can't seem to solve it

Discussão em 'Python' iniciado por Stack, Dezembro 1, 2024.

  1. Stack

    Stack Membro Participativo

    I am trying to create a local chatbot using LM Studio. I have the latest openai python library installed. However, when I run my 'app.py' file it states that

    line 25, in response = chat_with_gpt(human_input) line 17, in chat_with_gpt return completion.choices[0].message.content.strip() TypeError: 'NoneType' object is not subscriptable

    I can't see where I am going wrong. Any assistance would be greatly appreciated. Thanks.

    The full code is below:

    import openai

    openai.api_type = "open_ai"
    openai.base_url = "http://127.0.0.1:1234"
    openai.api_key = "NULL"



    def chat_with_gpt(prompt):
    completion = openai.chat.completions.create(
    messages = [
    {"role": "user",
    "content": prompt}
    ],
    model="llama3"
    )
    return completion.choices[0].message.content.strip()

    while True:
    human_input = input("Human: ")

    if human_input.lower() in ["quit", "exit", "bye"]:
    break

    response = chat_with_gpt(human_input)

    print("Chatbot: ", response)

    if __name__ == "__main__":
    while True:
    human_input = input("Human: ")

    if human_input.lower() in ["quit", "exit", "bye"]:
    break

    response = chat_with_gpt(human_input)

    print("Chatbot: ", response)


    I have reviewed the api guide from openAI but I can't solve the TypeError. If you haven't guessed I am a noob at this.

    Continue reading...

Compartilhe esta Página