0
1

第4回 StreamlitとLangChainとLM studioを使って自分専用LLMウェブアプリを作りたい!

Posted at

第3回

今回は要約アプリを実際に使ってみました。
参照URL:https://blog.streamlit.io/langchain-tutorial-3-build-a-text-summarization-app/
GitHub:https://github.com/s1932072/LangChain-tutorial-3/tree/main

main.py
import streamlit as st
from langchain import OpenAI
from langchain.docstore.document import Document
from langchain.text_splitter import CharacterTextSplitter
from langchain.chains.summarize import load_summarize_chain

def generate_response(txt):
    # Instantiate the LLM model
    llm = OpenAI(base_url="http://localhost:1234/v1", api_key="not-needed")
    # Split text
    text_splitter = CharacterTextSplitter(
        #chunk_size = 50,  # チャンクの文字数
        #chunk_overlap = 10,  # チャンクオーバーラップの文字数
    )
    texts = text_splitter.split_text(txt)
    # Create multiple documents
    docs = [Document(page_content=t) for t in texts]
    # Text summarization
    chain = load_summarize_chain(llm, chain_type='map_reduce')
    return chain.run(docs)

# Page title
st.set_page_config(page_title='🦜🔗 Text Summarization App')
st.title('🦜🔗 Text Summarization App')

# Text input
txt_input = st.text_area('Enter your text', '', height=200)

# Form to accept user's text input for summarization
result = []
with st.form('summarize_form', clear_on_submit=True):
    #openai_api_key = st.text_input('OpenAI API Key', type = 'password', disabled=not txt_input)
    submitted = st.form_submit_button('Submit')
    if submitted :
        with st.spinner('Calculating...'):
            response = generate_response(txt_input)
            result.append(response)
            #del openai_api_key
if len(result):
    st.info(response)

OpenAI APIをLocalLLMに変更しました。

このプログラムをでは、入力された文章を分割します。その後分割された文章毎に要約を行う。その後要約した各文章を順番に繋げることで、要約する。

一方でこのプログラムだと、使用している LLMmodelの入力可能token数を超えるとエラーが出てしまう。
そこで、

main.py
#chunk_size = 50,  # チャンクの文字数
#chunk_overlap = 10,  # チャンクオーバーラップの文字数

この部分でチャンクの文字数とオーバーラップを指定しましたが、エラーが出てしまったので、一旦コメントアウトしておきました。

収穫

今回行えた要約が、私のしたかったことの一部分になります。
私の目指すゴールの一つに、RSSやwebからニュース本文を取得して要約したい、がありました。

入力テキスト数によるエラーさえ回避できれば、完成です。

今後加筆修正する可能性もあります。
ご了承ください。

次回

この調子でstreamlitの記事を実装していきます。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1