Bruno Solano
Bruno Solano

Reputation: 1

Skill Alexa + GPT4 with Vector Store

I'm trying to modify Python code to invoke a custom assistant and its respective Vector Store in playground/assistant on OpenAI.

The idea is to be able to invoke a custom assistant from OpenAI, fed with data contained in its respective Vector Store, and to be able to talk to this data through the Echo Dot.

After reading the documentation, I saw that I could use the assistant_id parameter to call a custom assistant... But it gave me a "Response Error" response. I haven't found a solution yet.

The original project is by Alexandre, it is at: https://github.com/alexandremendoncaalvaro/skill-alexa-chatgpt4. But he's Brazilian and doesn't respond.

Below is my modified code for your review. Thank you very much in advance:

import os
import logging
import ask_sdk_core.utils as ask_utils
from ask_sdk_core.skill_builder import SkillBuilder
from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.dispatch_components import AbstractExceptionHandler
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model import Response
import openai

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

openai_api_key = "INSERT-APIKEY-OPENAI-HERE"
openai.api_key = openai_api_key

# Custom wizard parameters
custom_assistant_id = "YOUR_ASSISTANT_ID"
vector_store_id = "ID_DO_VECTOR_STORE"  # If applicable

messages = [
    {
        "role": "system",
        "content": "ALWAYS consult your knowledge base to provide correct answers.",
    }
]

class LaunchRequestHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_request_type("LaunchRequest")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        speak_output = "Here is your assistant! What is your question?"
        return handler_input.response_builder.speak(speak_output).ask(speak_output).response

class GptQueryIntentHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_intent_name("GptQueryIntent")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        query = handler_input.request_envelope.request.intent.slots["query"].value
        response = generate_gpt_response(query)
        return handler_input.response_builder.speak(response).ask("You can ask a new question or say: leave.").response

def generate_gpt_response(query):
    try:
        messages.append({"role": "user", "content": query})
        response = openai.ChatCompletion.create(
            model="gpt-4-mini", 
            messages=messages,
            max_tokens=700,
            temperature=0.8
        )
        reply = response.choices[0].message['content']
        messages.append({"role": "assistant", "content": reply})
        return reply
    except Exception as e:
        return f"Error generating response: {str(e)}"

class HelpIntentHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_intent_name("AMAZON.HelpIntent")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        speak_output = "How can I help you?"
        return handler_input.response_builder.speak(speak_output).ask(speak_output).response

class CancelOrStopIntentHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_intent_name("AMAZON.CancelIntent")(handler_input) or ask_utils.is_intent_name("AMAZON.StopIntent")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        speak_output = "Até logo!"
        return handler_input.response_builder.speak(speak_output).response

class SessionEndedRequestHandler(AbstractRequestHandler):
    def can_handle(self, handler_input):
        # type: (HandlerInput) -> bool
        return ask_utils.is_request_type("SessionEndedRequest")(handler_input)

    def handle(self, handler_input):
        # type: (HandlerInput) -> Response
        return handler_input.response_builder.response

class CatchAllExceptionHandler(AbstractExceptionHandler):
    def can_handle(self, handler_input, exception):
        # type: (HandlerInput, Exception) -> bool
        return True

    def handle(self, handler_input, exception):
        # type: (HandlerInput, Exception) -> Response
        logger.error(exception, exc_info=True)
        speak_output = "Sorry, I was unable to process your request."
        return handler_input.response_builder.speak(speak_output).ask(speak_output).response

sb = SkillBuilder()
sb.add_request_handler(LaunchRequestHandler())
sb.add_request_handler(GptQueryIntentHandler())
sb.add_request_handler(HelpIntentHandler())
sb.add_request_handler(CancelOrStopIntentHandler())
sb.add_request_handler(SessionEndedRequestHandler())
sb.add_exception_handler(CatchAllExceptionHandler())

lambda_handler = sb.lambda_handler()

Upvotes: 0

Views: 71

Answers (0)

Related Questions