File size: 1,098 Bytes
de3c2ee
 
 
c12d231
de3c2ee
 
 
 
 
 
 
 
 
 
 
 
 
 
c12d231
de3c2ee
 
c12d231
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
from utils import get_embeddings, search_document_annoy, \
    answer_with_gpt3_with_function_calls, transform_user_question, debug_print

# add input parameter: need api_key for demo
def get_response_from_model(user_input, top_k=3, annoy_metric='dot', model_name="gpt-3.5-turbo", user_query_preprocess=False):

    assert top_k > 0, 'k must be an integer greater than 0'

    if user_query_preprocess:
        chatgpt_question = transform_user_question(user_input, model_name)
    else:
        chatgpt_question = user_input
    debug_print("chatgpt_question: ", chatgpt_question)

    try:
        user_q_embedding = get_embeddings(chatgpt_question)
        document = search_document_annoy(user_q_embedding, top_k=top_k, metric=annoy_metric)
        reply = answer_with_gpt3_with_function_calls(document, user_input, model_name)
        print(f"returning reply: {reply}")
        return reply
    except Exception as e:
        print(f"returning error: {e}")
        return e._message
        # return "Error when trying to get embedding for the user query. Please try with a shorter question."