embedchain

Форк
0
105 строк · 3.3 Кб
1
import queue
2

3
import streamlit as st
4

5
from embedchain import App
6
from embedchain.config import BaseLlmConfig
7
from embedchain.helpers.callbacks import (StreamingStdOutCallbackHandlerYield,
8
                                          generate)
9

10

11
@st.cache_resource
12
def unacademy_ai():
13
    app = App()
14
    return app
15

16

17
app = unacademy_ai()
18

19
assistant_avatar_url = "https://cdn-images-1.medium.com/v2/resize:fit:1200/1*LdFNhpOe7uIn-bHK9VUinA.jpeg"
20

21
st.markdown(f"# <img src='{assistant_avatar_url}' width={35} /> Unacademy UPSC AI", unsafe_allow_html=True)
22

23
styled_caption = """
24
<p style="font-size: 17px; color: #aaa;">
25
🚀 An <a href="https://github.com/embedchain/embedchain">Embedchain</a> app powered with Unacademy\'s UPSC data!
26
</p>
27
"""
28
st.markdown(styled_caption, unsafe_allow_html=True)
29

30
with st.expander(":grey[Want to create your own Unacademy UPSC AI?]"):
31
    st.write(
32
        """
33
    ```bash
34
    pip install embedchain
35
    ```
36

37
    ```python
38
    from embedchain import App
39
    unacademy_ai_app = App()
40
    unacademy_ai_app.add(
41
        "https://unacademy.com/content/upsc/study-material/plan-policy/atma-nirbhar-bharat-3-0/",
42
        data_type="web_page"
43
    )
44
    unacademy_ai_app.chat("What is Atma Nirbhar 3.0?")
45
    ```
46

47
    For more information, checkout the [Embedchain docs](https://docs.embedchain.ai/get-started/quickstart).
48
    """
49
    )
50

51
if "messages" not in st.session_state:
52
    st.session_state.messages = [
53
        {
54
            "role": "assistant",
55
            "content": """Hi, I'm Unacademy UPSC AI bot, who can answer any questions related to UPSC preparation.
56
            Let me help you prepare better for UPSC.\n
57
Sample questions:
58
- What are the subjects in UPSC CSE?
59
- What is the CSE scholarship price amount?
60
- What are different indian calendar forms?
61
            """,
62
        }
63
    ]
64

65
for message in st.session_state.messages:
66
    role = message["role"]
67
    with st.chat_message(role, avatar=assistant_avatar_url if role == "assistant" else None):
68
        st.markdown(message["content"])
69

70
if prompt := st.chat_input("Ask me anything!"):
71
    with st.chat_message("user"):
72
        st.markdown(prompt)
73
        st.session_state.messages.append({"role": "user", "content": prompt})
74

75
    with st.chat_message("assistant", avatar=assistant_avatar_url):
76
        msg_placeholder = st.empty()
77
        msg_placeholder.markdown("Thinking...")
78
        full_response = ""
79

80
        q = queue.Queue()
81

82
        def app_response(result):
83
            llm_config = app.llm.config.as_dict()
84
            llm_config["callbacks"] = [StreamingStdOutCallbackHandlerYield(q=q)]
85
            config = BaseLlmConfig(**llm_config)
86
            answer, citations = app.chat(prompt, config=config, citations=True)
87
            result["answer"] = answer
88
            result["citations"] = citations
89

90
        results = {}
91

92
        for answer_chunk in generate(q):
93
            full_response += answer_chunk
94
            msg_placeholder.markdown(full_response)
95

96
        answer, citations = results["answer"], results["citations"]
97

98
        if citations:
99
            full_response += "\n\n**Sources**:\n"
100
            sources = list(set(map(lambda x: x[1], citations)))
101
            for i, source in enumerate(sources):
102
                full_response += f"{i+1}. {source}\n"
103

104
        msg_placeholder.markdown(full_response)
105
        st.session_state.messages.append({"role": "assistant", "content": full_response})
106

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.