0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Databricksでサービングする大規模言語モデルのGUIをChainlitで構築する

Last updated at Posted at 2023-06-09

思った以上にチャットbotになりました。

ベースの記事はこちらです。DatabricksにLangChain + OpenAI APIのLLMをデプロイしています。

以下のコードを記述します。

databricks.py
import os
from langchain import PromptTemplate, OpenAI, LLMChain
import chainlit as cl
from chainlit import on_message

import numpy as np 
from PIL import Image
import base64
import io

import os
import requests
import numpy as np
import pandas as pd

import json

def create_tf_serving_json(data):
    return {
        "inputs": {name: data[name].tolist() for name in data.keys()}
        if isinstance(data, dict)
        else data.tolist()
    }


def score_model(question):
  # 1. パーソナルアクセストークンを設定してください
  token = "<Databricksパーソナルアクセストークン>"

  # 2. モデルエンドポイントのURLを設定してください
  url = "<モデルエンドポイントのURL>"
  headers = {'Authorization': f'Bearer {token}',
             "Content-Type": "application/json",}

  dataset = pd.DataFrame({'question':[question]})

  ds_dict = (
        {"dataframe_split": dataset.to_dict(orient="split")}
        if isinstance(dataset, pd.DataFrame)
        else create_tf_serving_json(dataset)
    )
  data_json = json.dumps(ds_dict, allow_nan=True)
  response = requests.request(method="POST", headers=headers, url=url, data=data_json)
  if response.status_code != 200:
    raise Exception(
       f"Request failed with status {response.status_code}, {response.text}"
    )
  
  return response.json()


@on_message
def main(message: str):

    response = score_model(message)

    answer = response['predictions'][0]["answer"]
    source = response['predictions'][0]["source"]

    # Send a response back to the user
    cl.Message(
        content=f"{answer}\n\nソース: {source}",
    ).send()
chainlit run databricks.py -w

チャット🤖だー。

Screenshot 2023-06-09 at 16.42.44.png

Databricksクイックスタートガイド

Databricksクイックスタートガイド

Databricks無料トライアル

Databricks無料トライアル

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?