File size: 5,293 Bytes
6855b1e
8922c23
6855b1e
 
 
 
 
 
8922c23
6855b1e
 
 
 
 
 
 
 
 
 
 
 
 
a8c00ab
6855b1e
 
 
 
 
 
 
 
 
a8c00ab
6855b1e
 
843e3ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6855b1e
 
 
 
 
 
 
 
 
843e3ae
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6855b1e
39a05d7
6855b1e
39a05d7
6855b1e
 
 
 
 
 
 
3152165
6855b1e
de33d74
6855b1e
 
 
 
 
 
 
 
 
 
8922c23
6855b1e
 
 
 
 
39a05d7
 
f58ccf2
6855b1e
 
 
906b1c0
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import logging
import os
from time import asctime

import gradio as gr
from llama_index.core import Document, VectorStoreIndex

from generate_response import generate_chat_response_with_history, set_llm, is_search_query, condense_question, \
    generate_chat_response_with_history_rag_return_response
from web_search import search

API_KEY_PATH = "../keys/gpt_api_key.txt"
logger = logging.getLogger("agent_logger")
sourced = False
query = False
rag_similarity = False


def google_search_chat(message, history):
    condensed_question = condense_question(message, history)
    if is_search_query(condensed_question):
        search_results = search(message, condensed_question)
        print(f'Search results returned: {len(search_results)}')
        relevant_content = ""
        sources = ""
        for index, result in enumerate(search_results):
            relevant_content = relevant_content + "\n" + ''.join(result['text'])
            sources = sources + f'\n {index + 1}. ' + result['url']  # python is zero-indexed

        if relevant_content != "":
            documents = [Document(text=relevant_content)]
            index = VectorStoreIndex.from_documents(documents)
            print('Search results vectorized...')
            response = generate_chat_response_with_history_rag_return_response(index, message, history)

            # similar_str = "not calculated"
            # faithfulness_str = "not calculated"
            #
            # if rag_similarity:
            #     sim_evaluator = SemanticSimilarityEvaluator()
            #     faith_evaluator = FaithfulnessEvaluator(llm=get_llm())
            #     # condensed_context = condense_context(relevant_content)
            #     # logger.info("Calculating similarity...")
            #     # similar = sim_evaluator.evaluate(response=str(response),
            #     #                                   reference=condensed_context)
            #     logger.info("Calculating faithfulness...")
            #     faithfulness = faith_evaluator.evaluate_response(query=condensed_question, response=response)
            #     # similar_str = str(round((similar.score * 100), 2)) + "%"
            #     faithfulness_str = "Yes" if faithfulness.passing else "No"
            #
            # logger.info(f'**Search Query:** {condensed_question} \n **Faithfulness:** {faithfulness_str} \n '
            #             f'**Similarity:** {similar_str} \n **Sources used:** \n {sources}')

            response_text = []
            string_output = ""

            for text in response.response_gen:
                response_text.append(text)
                string_output = ''.join(response_text)
                yield string_output

            # if not sourced:
            #     pass
            # if sourced and not query and not rag_similarity:
            #     yield string_output + f'\n\n --- \n **Sources used:** \n {sources}'
            # if sourced and query and not rag_similarity:
            #     yield (string_output
            #            + f'\n\n --- \n **Search Query:** {condensed_question} '
            #              f'\n **Sources used:** \n {sources}')
            # if rag_similarity:
            #     yield (string_output
            #            + f'\n\n --- \n **Search Query:** {condensed_question} \n '
            #            # f'**Similarity of response to the sources [ℹ️]'
            #            # f'(https://en.wikipedia.org/wiki/Semantic_similarity):** {similar_str} \n'
            #              f'**Is response in source documents?**: {faithfulness_str}'
            #              f'\n **Sources used:** \n {sources}')

            print(f'Assistant Response: {string_output}')
        else:
            print(
                f'Assistant Response: Sorry, no search results found.')
            yield "Sorry, no search results found."

    else:
        yield from generate_chat_response_with_history(message, history)


if __name__ == '__main__':
    logging.root.setLevel(logging.INFO)
    filehandler = logging.FileHandler(f'agent_log_{asctime().replace(" ", "").lower().replace(":", "")}.log',
                                      'a')
    formatter = logging.Formatter('%(asctime)-15s::%(levelname)s::%(filename)s::%(funcName)s::%(lineno)d::%(message)s')
    filehandler.setFormatter(formatter)
    logger = logging.getLogger("agent_logger")
    for hdlr in logger.handlers[:]:  # remove the existing file handlers
        if isinstance(hdlr, logging.FileHandler):
            logger.removeHandler(hdlr)
    logger.addHandler(filehandler)  # set the new handler
    logger.setLevel(logging.INFO)

    api_key = os.getenv('gpt_api_key')

    # GPT - 4 Turbo. The latest GPT - 4 model intended to reduce cases of “laziness” where the model doesn’t complete
    # a task. Returns a maximum of 4,096 output tokens. Link:
    # https://openai.com/blog/new-embedding-models-and-api-updates
    set_llm(key=api_key, model="gpt-4-0125-preview", temperature=0)

    print("Launching Gradio ChatInterface for searchbot...")

    demo = gr.ChatInterface(fn=google_search_chat,
                            title="Search Assistant", retry_btn=None, undo_btn=None, clear_btn=None,
                            theme="soft")
    demo.launch(auth=('convo', 'session2024'))