common.title

Docs
Quantum Circuit
TYTAN CLOUD

QUANTUM GAMING


autoQAOA
Desktop RAG

Overview
Terms of service

Privacy policy

Contact
Research

Sign in
Sign up
common.title

Create an AI that teaches quantum computer programming with RAG

Yuichiro Minato

2024/04/13 07:41

Hello, I've been creating various things in this series, although I'm not sure how many times. This time, I'm trying something that includes programming code.

Learning to program quantum computers is challenging, isn't it? I'd like to make the LLM remember all about it, but it might not remember everything, so I'll use references. This time, I've used CSV.

pip install --quiet transformers accelerate langchain langchain-community sentence-transformers faiss-gpu pypdf gradio

from transformers import AutoTokenizer, pipeline

model_id = "mistralai/Mistral-7B-Instruct-v0.2"
tokenizer = AutoTokenizer.from_pretrained(model_id)

pipe = pipeline("text-generation", model=model_id, tokenizer=tokenizer, device=0, max_new_tokens=300)

I used the Mistral7B model. The data is read using pandas from a CSV file. This time, the file 'list2.csv' contains various explanations and codes.

from langchain_community.document_loaders import DataFrameLoader
from langchain_community.vectorstores import FAISS
from langchain_community.embeddings.huggingface import HuggingFaceEmbeddings
from langchain_text_splitters import CharacterTextSplitter
import pandas as pd

df = pd.read_csv("list2.csv")
loader = DataFrameLoader(df)

documents = loader.load()
text_splitter = CharacterTextSplitter(chunk_size=1000, chunk_overlap=0)
docs = text_splitter.split_documents(documents)

embeddings = HuggingFaceEmbeddings(
 model_name="intfloat/multilingual-e5-large"
)

db = FAISS.from_documents(docs, embeddings)
retriever = db.as_retriever()
print(db.index.ntotal)

from langchain_core.prompts import ChatPromptTemplate
from langchain_core.output_parsers import StrOutputParser
from langchain_core.runnables import RunnablePassthrough
from langchain.llms import HuggingFacePipeline

llm = HuggingFacePipeline(pipeline=pipe)

template = """あなたは量子コンピュータプログラミングの先生です。次のコンテクストに含まれる説明とコードを参照して質問に答えてください:

{context}

Question: {question}
"""

prompt = ChatPromptTemplate.from_template(template)

chain = (
 {"context": retriever, "question": RunnablePassthrough()}
 | prompt
 | llm
 | StrOutputParser()
)

This area is familiar. I passed the LLM built with a pipeline to RAG. I specified the prompt carefully in Japanese. And finally, Gradio.

import gradio as gr
import os

def add_text(history, text):
 history = history + [(text, None)]
 return history, gr.Textbox(value="", interactive=False)

def bot(history):
 query = history[-1][0]
 response = chain.invoke(query)  
 if "Answer:" in response:
  response = response.split("Answer:")[1]
 if "Question:" in response:
  response = response.split("Question:")[0]
 history[-1][1] = ""
 for character in response:
  history[-1][1] += character
  yield history

with gr.Blocks() as demo:
 chatbot = gr.Chatbot([])
 with gr.Row():
  txt = gr.Textbox(
   scale=4,
   show_label = False,
   container = False
  )
 clear = gr.Button("Clear")

txt_msg = txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue = False).then(bot, chatbot, chatbot)
 txt_msg.then(lambda: gr.Textbox(interactive = True), None, [txt], queue = False)
 clear.click(lambda: None, None, chatbot, queue=False)

demo.queue()  
demo.launch(share=True)

You can do it by just reusing the minimal configuration in various ways. It works like this through the interface.

Moreover, it properly presents the code and explains what's inside the code as well.

© 2025, blueqat Inc. All rights reserved