Skip to content

Instantly share code, notes, and snippets.

@inf3rtil
Created July 2, 2025 06:55
Show Gist options
  • Save inf3rtil/2a649927a37f0ba2188f79e5494a3a42 to your computer and use it in GitHub Desktop.
Save inf3rtil/2a649927a37f0ba2188f79e5494a3a42 to your computer and use it in GitHub Desktop.
LLM on Rigol DP800
from telegram import Update
from telegram.ext import Application, CommandHandler, MessageHandler, filters, ContextTypes
import pyvisa
import ollama
BOT_TOKEN = "meu token gerado no botfather"
system_context="""
You are a natural language to fixed command converter for a power supply (channel).
1. translate from portuguese to english and change all string numbers to digits values if necessary
2. You ONLY answer with valid commands from this list:
- "OUTP CH{X},{Y}": when I say something like "Turn ON or OFF power supply/channel X" where Y is ON or OFF
- "SOUR{X}:VOLT {Y}": when I say something like "Change voltage X to Y volts" where Y is a value betwen 0 and 30.000
3. Never explain, never add extra text. X is a value between 1 and 3, for any value out of range, answer with "ERROR"
example: Voltage 2 should 10.567 = SOUR2:VOLT 10.567
example: turn on power 2 = OUTP CH2,ON
answer without quotation marks
"""
ollama.create(model='rigol', from_='mistral:instruct', system=system_context)
rm = pyvisa.ResourceManager('@py')
print(rm.list_resources())
device = rm.open_resource(rm.list_resources()[0])
print(device.query('*IDN?'))
async def handle_message(update: Update, context: ContextTypes.DEFAULT_TYPE):
text = update.message.text.lower()
write_command(text)
response = "Comando enviado!"
await update.message.reply_text(response)
async def error(update: Update, context: ContextTypes.DEFAULT_TYPE):
print(f"Update {update} caused error {context.error}")
def write_command(text):
response = ollama.generate(
model='rigol',
prompt=text,
options={
"temperature": 0.1
}
)
print(response['response'])
device.write(response['response'])
def main():
app = Application.builder().token(BOT_TOKEN).build()
app.add_handler(MessageHandler(filters.TEXT, handle_message))
app.add_error_handler(error)
print("Polling...")
app.run_polling(poll_interval=3)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment