スポンサーリンク

Qwen3を使ってみる

Qwen3-8B

中国産のLLM、Qwen3。

今使っているのが NVIDIA GeForce RTX 3070 Ti 。

パラメータ数1Bあたり約1GBのVRAMが必要らしいので、 Qwen3-8B が使える。

https://www.linkedin.com/pulse/qwen3-self-hosting-guide-vllm-sglang-maksym-huczynski-i4v2f?utm_source=chatgpt.com

ollama pull qwen3:8b
from langchain_ollama import OllamaLLM

import re

llm = OllamaLLM(model="qwen3:8b")
response = llm.invoke("こんにちは")

response_without_think = re.sub(r"<think>.*?</think>", "", response, flags=re.DOTALL)

print(response_without_think)

出力

こんにちは!お手伝いできますか?何か質問やご希望があれば、いつでもお知らせくださいね。😊

応用

結構理想的な答えが返ってきたので、ユーザーの指示通りにPCを操作する超簡易的スクリプトを組んでみた。流石に重要なフォルダを削除などされると困るので、スクリプトを確認してから動作させる。

from langchain_ollama import OllamaEmbeddings
from langchain_community.vectorstores import FAISS
from langchain_community.document_loaders import TextLoader
from langchain_ollama import OllamaLLM
from langchain.text_splitter import RecursiveCharacterTextSplitter
from langchain.chains import RetrievalQA

import re
import subprocess

def GetPythonCodeLLM( llm,shiji ):
   response = llm.invoke("あなたはWindows上で動作するPythonコード生成器です。次の指示を実行可能なPythonコードを作成せよ。出力にPythonコード以外の内容を一切含めてはならない。コードはMarkdownのコードブロックで囲むこと。以下指示:"+ shiji )

   # <think>...</think> を削除
   response_without_think = re.sub(r"<think>.*?</think>", "", response, flags=re.DOTALL)
   
   return response_without_think

def extract_python_code_blocks(text):
    """
    与えられた文字列から、 ```python ~ ``` で囲まれた範囲のコードをリストで返す。
    """
    pattern = r"```python\s*(.*?)```"
    match = re.search(pattern, text, re.DOTALL)
    return match.group(1) if match else None

llm = OllamaLLM(model="qwen3:8b")



instructions = input("指示:").strip().lower()

response = GetPythonCodeLLM(llm,instructions)
code = extract_python_code_blocks(response)

print(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>")
print(code)
print("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<")
confirmation = input("処理を続行しますか? [Y/n]: ").strip().lower()

if confirmation in ['', 'y', 'yes']:

    with open('job.py', 'w') as file:
        file.write(code)
    subprocess.run(["python", "job.py"])

else:
    print("処理を中止しました。")

 

指示:C:\test2 内のファイル一覧を表示してください
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import os

path = r'C:\test2'
for item in os.listdir(path):
item_path = os.path.join(path, item)
if os.path.isfile(item_path):
print(item)

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
処理を続行しますか? [Y/n]: Y
freetypetest.pbm
local.html
meiryob.ttc
s01.gif

 

時々、リストをそのまま出力することがある。

 

指示:C:\test2 内のファイル一覧を表示してください
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
import os
print(os.listdir(r'C:\test2'))

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
処理を続行しますか? [Y/n]: Y
['freetypetest.pbm', 'local.html', 'meiryob.ttc', 's01.gif']

 

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)


この記事のトラックバックURL: