Un'introduzione operativa agli LLM¶
Con accesso a un LLM locale attraverso un server di API¶
Luca Mari, febbraio 2024
[i file di questa attività: base.ipynb, baseutils.py, base.py]
Obiettivi: comprendere la logica dell'accesso a un LLM attraverso una API.
Precompetenze: basi di Python.
embedding_layer.embedding_dim
Al momento un sistema semplice per eseguire sul proprio computer un LLM e accedere a esso attraverso una API è LM Studio:
- scaricare da https://lmstudio.ai e installare LM Studio
- eseguire LM Studio e seguendo le indicazioni nel programma:
- scaricare dalla rete un LLM (per esempio TheBloke mistral openorca 7B Q4_0 gguf)
- caricare il LLM
- mettere in esecuzione il server
Occorre ora creare un ambiente di lavoro Python, supponiamo con VSCode:
- installare un interprete Python
- scaricare da https://code.visualstudio.com/download e installare VSCode
- eseguire VSCode e attivare le estensioni per Python e Jupyter
- ancora in VSCode:
- creare una cartella di lavoro e renderla la cartella corrente:
- copiare nella cartella questo notebook e il file
baseutils.py
e aprire il notebook - creare un ambiente virtuale locale Python (Select Kernel | Python Environments | Create Python Environment | Venv, e scegliere un interprete Python):
- installare il modulo Python richiesto, eseguendo dal terminale:
pip install openai
Eseguendo l'applicazione Python base.py
si attiva poi una semplice chat.
Per prima cosa, importiamo il modulo Python che contiene le funzioni per consentire un accesso "di alto livello" al LLM in esecuzione in LM Studio e quindi attiviamo la connessione al LLM.
from baseutils import llm
llm = llm()
A questo punto siamo già pronti per fare una domanda al LLM, ricevere e visualizzare la risposta.
prompt = "What are the main features of the Bayesian interpretation of probability? Please answer in one or two sentences."
answer = llm.request(prompt)
print(answer)
ChatCompletion(id='chatcmpl-jd65f2zin1wviybz0hy9', choices=[Choice(finish_reason='stop', index=0, logprobs=None, message=ChatCompletionMessage(content="The Bayesian interpretation of probability defines it as a degree of belief or certainty in an event's occurrence, which can be updated with new evidence. It contrasts with frequentist probability, which is based on the long-run frequency of events in repeated trials.", role='assistant', function_call=None, tool_calls=None))], created=1710409587, model='/home/lucamari/.local/share/nomic.ai/GPT4All/TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF/mixtral-8x7b-instruct-v0.1.Q8_0.gguf', object='chat.completion', system_fingerprint=None, usage=CompletionUsage(completion_tokens=55, prompt_tokens=272, total_tokens=327))
La risposta non è molto leggibile, perché è un oggetto Python. Per curiosità, ne possiamo visualizzare il contenuto come un oggetto JSON.
from baseutils import jprint
jprint(answer)
{ "id": "chatcmpl-jd65f2zin1wviybz0hy9", "choices": [ { "finish_reason": "stop", "index": 0, "logprobs": null, "message": { "content": "The Bayesian interpretation of probability defines it as a degree of belief or certainty in an event's occurrence, which can be updated with new evidence. It contrasts with frequentist probability, which is based on the long-run frequency of events in repeated trials.", "role": "assistant", "function_call": null, "tool_calls": null } } ], "created": 1710409587, "model": "/home/lucamari/.local/share/nomic.ai/GPT4All/TheBloke/Mixtral-8x7B-Instruct-v0.1-GGUF/mixtral-8x7b-instruct-v0.1.Q8_0.gguf", "object": "chat.completion", "system_fingerprint": null, "usage": { "completion_tokens": 55, "prompt_tokens": 272, "total_tokens": 327 } }
Per visualizzare solo il testo della risposta, usiamo la funzione bprint
dal modulo utils
.
from baseutils import bprint
bprint(answer)
The Bayesian interpretation of probability defines it as a degree of belief or certainty in an event's occurrence, which can be updated with new evidence. It contrasts with frequentist probability, which is based on the long-run frequency of events in repeated trials.
Ancora meglio, possiamo attivare la funzione per visualizzare i token progressivamente, mano a mano che vengono generati.
from baseutils import aprint
aprint(llm.request(prompt, stream=True))
The Bayesian interpretation of probability posits that probability is a measure of belief or confidence in the occurrence of an event, which can be updated as new evidence becomes available through the application of Bayes' theorem. It contrasts with frequentist interpretations that define probability based on long -run frequencies in repeated trials.