Closed5

LMQL

laisolaiso
  • LLM(GPT)に入力するプロンプトをSQLみたいな宣言型のクエリで記述して裏でpythonプログラムにコンパイルでしてテキストを吐いて送信してやろうという豪快なやつ
  • 人間が理解できるクエリをLLM向けに最適化されたプロンプトにすることでプロンプト部分の効率を高められるらしい

https://github.com/eth-sri/lmql

チューリッヒ工科大学の研究グループがMar 4, 2023に公開した。この論文の成果物っぽい

https://arxiv.org/abs/2212.06094v1

解説

https://note.com/mahlab/n/n11b15b323c87

laisolaiso

Hello World

hello.lmql
argmax "Hello[WHO]" from "openai/text-ada-001" where len(WHO) < 10
> lmql run hello.lmql

Hello everyone

 valid=True, final=var
hello_compiled.py
❯ cat hello_compiled.py                                 
import lmql
@lmql.compiled_query(output_variables=["WHO"])
async def query(WHO=None,argmax=None,context=None,len=None):
   yield lmql.runtime_support.context_call("set_model", 'openai/text-ada-001')
   yield lmql.runtime_support.context_call("set_decoder", 'argmax', )
   # where
   intm0 = lmql.ops.Lt([lmql.ops.LenOp([lmql.runtime_support.Var('WHO')]), 10])
   yield lmql.runtime_support.context_call("set_where_clause", intm0)
   # prompt
   (yield lmql.runtime_support.interrupt_call('query', f'Hello[WHO]'))
   WHO = (yield lmql.runtime_support.context_call('get_var', 'WHO'))
   yield ('result', (yield lmql.runtime_support.context_call("get_return_value", ())))

ReactのJSXみたいなアーキテクチャだ……

laisolaiso

Chat系モデル

Chat系モデルには大半のクエリに対応してない(以下argmax)。

argmax "Hello[WHO]" from "openai/gpt-3.5-turbo" where len(WHO) < 10

以下のエラーが出る

AssertionError: Chat API models do not support advanced constraining of the output, please use no or less complicated constraints.

API仕様が違うからまだ対応してないだけかもしれない

sample()はいける

sample(temperature=0.8)
   "A list of things not to forget when going to the sea (not travelling): \n"
   for i in range(5):
      "-[THING]"
from
   'chatgpt'
where
   STOPS_AT(THING, "\n")
A list of things not to forget when going to the sea (not travelling): 
- Sunscreen 
- Sunglasses 
- A hat 
- A beach towel 
- Swimwear 

 valid=True, final=fin

コンパイル結果

import lmql
@lmql.compiled_query(output_variables=["THING"])
async def query(STOPS_AT=None,THING=None,context=None,range=None,sample=None):
   yield lmql.runtime_support.context_call("set_model", 'openai/gpt-3.5-turbo')
   yield lmql.runtime_support.context_call("set_decoder", 'sample', temperature=0.8)
   # where
   yield lmql.runtime_support.context_call("set_where_clause", lmql.ops.StopAtOp([lmql.runtime_support.Var('THING'), '\n']))
   # prompt
   (yield lmql.runtime_support.interrupt_call('query', f'A list of things not to forget when going to the sea (not travelling): \n'))
   for i in range(5):
       (yield lmql.runtime_support.interrupt_call('query', f'-[THING]'))
       THING = (yield lmql.runtime_support.context_call('get_var', 'THING'))
   yield ('result', (yield lmql.runtime_support.context_call("get_return_value", ())))
laisolaiso

マルチバイト処理

sample(temperature=0.8)
   "日本がAIで成長するために必要な5つのこと: \n"
   "[LIST]"
from
   'chatgpt'
output
日本がAIで成長するために必要な5つのこと: 
 1. AIの����������を�����人��の�����
2. AI�����の�����������の�����                                                                                                                                                               
3. ����のデータの����と�����のた��のインフ ラストラクチャーの����

日本語入れたら出力化けていた。

https://github.com/eth-sri/lmql/issues/38

修正してみてる

https://github.com/eth-sri/lmql/pull/39

このスクラップは2023/05/13にクローズされました