0
0

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

Last updated at Posted at 2023-12-31

第2回! 簡単な実装編

初めの一歩

ここではLM StudioでローカルLLMサーバーを立て、そこにアクセスすることで動く簡単なChatBotを作成しました。

プログラムの内容はほぼ以下のURLと同じです。
https://blog.streamlit.io/langchain-tutorial-1-build-an-llm-powered-app-in-18-lines-of-code/

変わった点はOpenAI APIを使用せず、ローカルLLMに繋いだ点のみです。

プログラムはGitHubにもあげました。
https://github.com/s1932072/LangChain-tutorial-1-Build-an-LLM-powered-app-in-18-lines-of-code

main.py
import streamlit as st
from langchain.llms import OpenAI

st.title('🦜🔗 Quickstart App')



def generate_response(input_text):
    # ローカルのLLMエンドポイントに接続するための設定
    llm = OpenAI(base_url="http://localhost:1234/v1", api_key="not-needed", temperature=0.7)
    st.info(llm(input_text))

with st.form('my_form'):
    text = st.text_area('Enter text:', '日本の首都はどこですか?')
    submitted = st.form_submit_button('Submit')
    # OpenAI APIキーのバリデーションをコメントアウト
    # if not openai_api_key.startswith('sk-'):
    #     st.warning('Please enter your OpenAI API key!', icon='⚠')
    if submitted:
        generate_response(text)

実際の画面スクリーンショット 2023-12-31 17.50.11.png

このような形でテキストを書き込んでsubmitを押せば実行できます。

これでLLMとwebブラウザで会話できるようになりました。

次回の予定

この調子でstreamlitの記事をLocal LLMで実施できるようにしていきます。
反響があればLM Studio の話や使用しているLLMmodelの話をしようと思います。

0
0
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
0