gizemsarsinlar commited on
Commit
f46186a
1 Parent(s): b185d1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +73 -105
app.py CHANGED
@@ -1,112 +1,80 @@
 
 
 
1
  import os
2
- from typing import List
3
- import fitz # PyMuPDF
4
- from langchain.embeddings.openai import OpenAIEmbeddings
5
- from langchain.text_splitter import RecursiveCharacterTextSplitter
6
- from langchain.vectorstores import Chroma
7
- from langchain.chains import ConversationalRetrievalChain
8
- from langchain.chat_models import ChatOpenAI
9
- from langchain.docstore.document import Document
10
- from langchain.memory import ChatMessageHistory, ConversationBufferMemory
11
- import chainlit as cl
12
-
13
- # OpenAI API anahtarını ayarla
14
- os.environ["OPENAI_API_KEY"] = "OPENAI_API_KEY"
15
-
16
- # Metin bölme işlemi için ayarlar
17
- text_splitter = RecursiveCharacterTextSplitter(chunk_size=1000, chunk_overlap=100)
18
-
19
- # PDF dosyasını metne dönüştürme fonksiyonu
20
- def extract_text_from_pdf(pdf_path: str) -> str:
21
- doc = fitz.open(pdf_path)
22
- text = ""
23
- for page in doc:
24
- text += page.get_text()
25
- return text
26
-
27
- @cl.on_chat_start
28
- async def on_chat_start():
29
- files = None
30
-
31
- # Kullanıcının dosya yüklemesini bekle
32
- while files is None:
33
- files = await cl.AskFileMessage(
34
- content="Please upload a PDF file to begin!",
35
- accept=["application/pdf"], # PDF dosyalarını kabul et
36
- max_size_mb=20,
37
- timeout=180,
38
- ).send()
39
-
40
- file = files[0]
41
-
42
- msg = cl.Message(content=f"Processing `{file.name}`...")
43
- await msg.send()
44
-
45
- # PDF dosyasını metne dönüştür
46
- text = extract_text_from_pdf(file.path)
47
-
48
- # Metni böl
49
- texts = text_splitter.split_text(text)
50
-
51
- # Her bir metin parçası için metadata oluştur
52
- metadatas = [{"source": f"{i}-pl"} for i in range(len(texts))]
53
-
54
- # Chroma vektör depolama oluştur
55
- embeddings = OpenAIEmbeddings()
56
- docsearch = await cl.make_async(Chroma.from_texts)(
57
- texts, embeddings, metadatas=metadatas
58
- )
59
 
60
- # Sohbet geçmişi ve hafıza yönetimi
61
- message_history = ChatMessageHistory()
62
- memory = ConversationBufferMemory(
63
- memory_key="chat_history",
64
- output_key="answer",
65
- chat_memory=message_history,
66
- return_messages=True,
67
- )
68
 
69
- # Chroma vektör depolamayı kullanan bir zincir oluştur
70
- chain = ConversationalRetrievalChain.from_llm(
71
- ChatOpenAI(model_name="gpt-3.5-turbo", temperature=0, streaming=True),
72
- chain_type="stuff",
73
- retriever=docsearch.as_retriever(),
74
- memory=memory,
75
- return_source_documents=True,
76
- )
 
 
 
 
 
 
 
 
 
 
 
77
 
78
- # Sistemin hazır olduğunu kullanıcıya bildir
79
- msg.content = f"Processing `{file.name}` done. You can now ask questions!"
80
- await msg.update()
81
 
82
- # Zinciri kullanıcı oturumunda sakla
83
- cl.user_session.set("chain", chain)
84
 
85
- @cl.on_message
86
  async def main(message: cl.Message):
87
- chain = cl.user_session.get("chain") # type: ConversationalRetrievalChain
88
- cb = cl.AsyncLangchainCallbackHandler()
89
-
90
- # Kullanıcının mesajını işle
91
- res = await chain.acall(message.content, callbacks=[cb])
92
- answer = res["answer"]
93
- source_documents = res["source_documents"] # type: List[Document]
94
-
95
- text_elements = [] # type: List[cl.Text]
96
-
97
- if source_documents:
98
- for source_idx, source_doc in enumerate(source_documents):
99
- source_name = f"source_{source_idx}"
100
- # Mesajda gösterilecek metin öğesini oluştur
101
- text_elements.append(
102
- cl.Text(content=source_doc.page_content, name=source_name, display="side")
103
- )
104
- source_names = [text_el.name for text_el in text_elements]
105
-
106
- if source_names:
107
- answer += f"\nSources: {', '.join(source_names)}"
108
- else:
109
- answer += "\nNo sources found"
110
-
111
- # Sonucu kullanıcıya gönder
112
- await cl.Message(content=answer, elements=text_elements).send()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # You can find this code for Chainlit python streaming here (https://docs.chainlit.io/concepts/streaming/python)
2
+
3
+ # OpenAI Chat completion
4
  import os
5
+ from openai import AsyncOpenAI # importing openai for API usage
6
+ import chainlit as cl # importing chainlit for our app
7
+ from chainlit.prompt import Prompt, PromptMessage # importing prompt tools
8
+ from chainlit.playground.providers import ChatOpenAI # importing ChatOpenAI tools
9
+ from dotenv import load_dotenv
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
+ load_dotenv()
 
 
 
 
 
 
 
12
 
13
+ # ChatOpenAI Templates
14
+ system_template = """You are a helpful assistant who always speaks in a pleasant tone!
15
+ """
16
+
17
+ user_template = """{input}
18
+ Think through your response step by step.
19
+ """
20
+
21
+
22
+ @cl.on_chat_start # marks a function that will be executed at the start of a user session
23
+ async def start_chat():
24
+ settings = {
25
+ "model": "gpt-3.5-turbo",
26
+ "temperature": 0,
27
+ "max_tokens": 500,
28
+ "top_p": 1,
29
+ "frequency_penalty": 0,
30
+ "presence_penalty": 0,
31
+ }
32
 
33
+ cl.user_session.set("settings", settings)
 
 
34
 
 
 
35
 
36
+ @cl.on_message # marks a function that should be run each time the chatbot receives a message from a user
37
  async def main(message: cl.Message):
38
+ settings = cl.user_session.get("settings")
39
+
40
+ client = AsyncOpenAI()
41
+
42
+ print(message.content)
43
+
44
+ prompt = Prompt(
45
+ provider=ChatOpenAI.id,
46
+ messages=[
47
+ PromptMessage(
48
+ role="system",
49
+ template=system_template,
50
+ formatted=system_template,
51
+ ),
52
+ PromptMessage(
53
+ role="user",
54
+ template=user_template,
55
+ formatted=user_template.format(input=message.content),
56
+ ),
57
+ ],
58
+ inputs={"input": message.content},
59
+ settings=settings,
60
+ )
61
+
62
+ print([m.to_openai() for m in prompt.messages])
63
+
64
+ msg = cl.Message(content="")
65
+
66
+ # Call OpenAI
67
+ async for stream_resp in await client.chat.completions.create(
68
+ messages=[m.to_openai() for m in prompt.messages], stream=True, **settings
69
+ ):
70
+ token = stream_resp.choices[0].delta.content
71
+ if not token:
72
+ token = ""
73
+ await msg.stream_token(token)
74
+
75
+ # Update the prompt object with the completion
76
+ prompt.completion = msg.content
77
+ msg.prompt = prompt
78
+
79
+ # Send and close the message stream
80
+ await msg.send()