### Install Core Libraries Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_JSON.ipynb Installs the 'accelerate' and 'bitsandbytes' libraries required for efficient model loading and execution. Use this at the beginning of your environment setup. ```python #%%capture !pip install accelerate -q !pip install -i https://pypi.org/simple/ bitsandbytes -q ``` -------------------------------- ### Install Libraries for Bielik Model Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_simple_examples.ipynb Installs the 'accelerate' and 'bitsandbytes' libraries. Use the '-q' flag for quieter installation or '%%capture' to suppress output. ```bash !pip install accelerate !pip install -i https://pypi.org/simple/ bitsandbytes ``` -------------------------------- ### Install vLLM and Outlines Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(AWQ)_structured_output.ipynb Installs the vLLM and Outlines libraries. Ensure you have a compatible environment, especially for GPU acceleration. ```python %%capture !pip install vllm outlines ``` -------------------------------- ### Install Dependencies Source: https://github.com/speakleash/bielik-how-to-start/blob/main/contract_enhancer/README.md Installs necessary project dependencies listed in the requirements.txt file. Ensure you have Python and pip installed. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install Weaviate Client and Dependencies Source: https://github.com/speakleash/bielik-how-to-start/blob/main/weaviate/notebooks/0-import.ipynb Installs the Weaviate client and other required Python packages. Run this before importing any Weaviate modules. ```python !pip install weaviate-client !pip install requests !pip install datasets ``` -------------------------------- ### Install Justext Library Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_JSON.ipynb Installs the justext library for text extraction. Use this command in your environment to set up the necessary package. ```bash ! ``` -------------------------------- ### Install Quantization Libraries Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Instruct_QUANT_Tests.ipynb Installs necessary libraries for quantization, including accelerate, optimum, auto-gptq, and ctransformers with CUDA support. ```python %%capture !pip install accelerate -q !pip install optimum -q !pip install auto-gptq -q !pip install ctransformers[cuda] ``` -------------------------------- ### Hello World Example with Higher Temperature Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_simple_examples.ipynb A 'Hello World' example demonstrating response generation with a temperature of 1.0, allowing for more creative and varied output. The max_tokens is set to 256. ```python temperature = 1.0 max_tokens = 256 prompt = "Witaj świecie!" generate(prompt) ``` -------------------------------- ### Install justext Library Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_sentiment_analysis.ipynb Installs the 'justext' library, which is required for extracting clean text from web pages. Use this command in your environment before running the text extraction code. ```bash # Instalacja niezbędnych bibliotek !pip install justext -q ``` -------------------------------- ### Install Hugging Face CLI Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_sentiment_analysis.ipynb Installs the 'huggingface_hub' library with CLI support, which is necessary for logging into Hugging Face. This is a prerequisite for accessing gated models. ```python !pip install -U "huggingface_hub[cli]" -q ``` -------------------------------- ### Install Dependencies and LocalTunnel Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Streamlit_simple_app_tunnel_GGUF_Q4.ipynb Installs required Python libraries for Streamlit, model loading, and GPU acceleration. It also installs localtunnel for exposing the Streamlit app to the internet. ```bash %%capture !pip install -q streamlit accelerate transformers ctransformers[cuda] !npm install -g localtunnel ``` -------------------------------- ### Install Python Packages Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_RAG.ipynb Installs the necessary Python packages for the project using pip. Ensure you have pip installed and a stable internet connection. ```python !pip install requests !pip install accelerate !pip install -i https://pypi.org/simple/ bitsandbytes !pip install chromadb !pip install sentence-transformers !pip install PyMuPDF ``` -------------------------------- ### Install Dependencies for Model Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_sentiment_analysis.ipynb Installs the 'accelerate' and 'bitsandbytes' libraries required for running the model efficiently, especially with quantization. Use the specified PyPI index for 'bitsandbytes'. ```python !pip install accelerate -q !pip install -i https://pypi.org/simple/ bitsandbytes -q ``` -------------------------------- ### Install Hugging Face Hub CLI Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_JSON.ipynb Installs the Hugging Face Hub command-line interface, which is necessary for logging in and managing authentication tokens for gated models. ```bash !pip install -U "huggingface_hub[cli]" ``` -------------------------------- ### Install vLLM Library Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Instruct_QUANT_Tests.ipynb Installs the vLLM library, which is required for efficient LLM inference. This command should be run in a Colab environment. ```bash !pip install vllm ``` -------------------------------- ### Install bitsandbytes for Colab Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_Text_Improvement.ipynb Installs or updates the `bitsandbytes` library, which is often required for efficient model loading and execution, especially in environments like Google Colab. Use the `-q` flag for quiet installation. ```python !pip install -U bitsandbytes -q ``` -------------------------------- ### Start Weaviate, Vectorizers, and Ollama Containers Source: https://github.com/speakleash/bielik-how-to-start/blob/main/weaviate/README.md Use this command to launch the necessary Docker containers for Weaviate, vectorizers, and the Ollama module. ```sh docker compose up ``` -------------------------------- ### Install Libraries for Model Loading Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Data_Generation_and_Fewshot_Prompting_(4_bit).ipynb Installs the 'accelerate' and 'bitsandbytes' libraries. 'accelerate' is required for multi-GPU/TPU efficiency, and 'bitsandbytes' enables loading models with reduced precision (quantization) to save memory. ```python !pip install accelerate -q !pip install -i https://pypi.org/simple/ bitsandbytes -q ``` -------------------------------- ### Install Ollama in Google Colab Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_Ollama_integration.ipynb Execute this command in Google Colab to download and install Ollama. This is a prerequisite for running Ollama services in the Colab environment. ```python !curl https://ollama.ai/install.sh | sh ``` -------------------------------- ### Install Core AI/NLP Packages Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_OlimpiadaAI_RAG.ipynb Installs essential Python packages for AI and NLP development using pip. Ensure you have Python and pip installed. ```python !pip install langchain !pip install langchain-openai !pip install chromadb !pip install sentence-transformers !pip install justext !pip install requests ``` -------------------------------- ### Install Sentence Transformers Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_RAG.ipynb Installs the `sentence-transformers` library, which is essential for generating embeddings for text data. This package has dependencies on `transformers`, `torch`, and `tqdm`. ```bash pip install sentence-transformers ``` -------------------------------- ### Initialize ChromaDB Client and Collection Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_OlimpiadaAI_RAG.ipynb Initializes a persistent ChromaDB client and gets or creates a collection named 'oai_naive_rag' with cosine similarity specified for metadata. Assumes CHROMA_PATH is defined elsewhere. ```python import chromadb # Zainicjalizowanie bazy wektorowej client = chromadb.PersistentClient(path=CHROMA_PATH) collection = client.get_or_create_collection( name="oai_naive_rag", metadata={"hnsw:space": "cosine"} ) ``` -------------------------------- ### Install Dependencies for Streamlit App Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Streamlit_simple_app_tunnel_4bit.ipynb Installs necessary Python libraries for Streamlit, model acceleration, and quantization, as well as Node.js for localtunnel. This should be run before the application code. ```bash %%capture !pip install -q streamlit accelerate transformers bitsandbytes !npm install -g localtunnel ``` -------------------------------- ### Install Python Libraries for Ollama API Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Ollama_integration.ipynb Installs the 'httpx' and 'requests' libraries, which are used for making HTTP requests to the Ollama API. Choose 'httpx' if you are unsure which to use. ```python !pip install -qq httpx requests ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_RAG.ipynb Installs a comprehensive list of Python packages required for the Bielik RAG project, including web framework components, vector database, and NLP models. Ensure pip is up-to-date before running. ```bash pip install pypika monotonic mmh3 websockets uvloop python-dotenv overrides orjson opentelemetry-util-http opentelemetry-proto humanfriendly httptools h11 bcrypt backoff asgiref watchfiles uvicorn starlette posthog opentelemetry-exporter-otlp-proto-common opentelemetry-api httpcore coloredlogs opentelemetry-semantic-conventions opentelemetry-instrumentation onnxruntime kubernetes httpx fastapi opentelemetry-sdk opentelemetry-instrumentation-asgi opentelemetry-instrumentation-fastapi opentelemetry-exporter-otlp-proto-grpc chromadb ``` -------------------------------- ### Load Bielik Model and Set Up Chat Template with Unsloth Source: https://context7.com/speakleash/bielik-how-to-start/llms.txt Installs Unsloth and related libraries, loads the Bielik-4.5B-v3.0-Instruct model, and configures a custom chat template for advanced interactions. ```python # Instalacja: pip install unsloth && pip install --no-deps xformers trl peft accelerate bitsandbytes # Wymagany szablon chatu: curl -LO https://raw.githubusercontent.com/speakleash/bielik-tools/refs/heads/main/tools/bielik_advanced_chat_template.jinja from unsloth import FastLanguageModel from unsloth.chat_templates import get_chat_template model, tokenizer = FastLanguageModel.from_pretrained( model_name="speakleash/Bielik-4.5B-v3.0-Instruct", max_seq_length=2048, dtype=None, load_in_4bit=False, ) unsloth_template = open("bielik_advanced_chat_template.jinja").read() tokenizer = get_chat_template( tokenizer, chat_template=(unsloth_template, "<|im_end|>"), mapping={"role": "from", "content": "value", "user": "human", "assistant": "gpt"}, map_eos_token=True, ) FastLanguageModel.for_inference(model) ``` -------------------------------- ### Initialize ChatOpenAI Client and Invoke Model Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_OlimpiadaAI_RAG.ipynb Sets up the ChatOpenAI client with model details and API key. Prepares system and human messages for the model and invokes it to get a response. Ensure MODEL_URL, MODEL_NAME, MODEL_API_KEY, and TEMPERATURE are defined. ```python from langchain_core.messages import AIMessage, HumanMessage, SystemMessage from langchain_openai import ChatOpenAI # Wywołanie modelu client = ChatOpenAI( base_url=MODEL_URL, model=MODEL_NAME, api_key=MODEL_API_KEY, ) # Przygotowanie danych dla modelu messages = [ SystemMessage('Odpowiadaj krótko i na temat, stosuj profesjonalny ton odpowiedzi. Zawsze zaczynaj odpowiedź od "Szanowany Panie" i bądź bardzo wylewny w swoich wypowiedziach.'), HumanMessage('Kim jesteś i kto Cię stworzył?') ] # Wysłanie zapytania do modelu response = client.invoke( input=messages, temperature=TEMPERATURE ) # Spradzenie kompletności odpowiedzi if response and hasattr(response, 'content'): print(response.content) ``` -------------------------------- ### Interact with Bielik LLM via Ollama CLI Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Ollama_integration.ipynb Example of interacting with the Bielik LLM model directly in the console after it has been loaded by Ollama. ```shell (base) marcinwatroba@DataScienceServer:~$ ollama run SpeakLeash/bielik-7b-instruct-v0.1-gguf >>> Ile to jest 2+4? 2 plus 4 równa się 6. >>> Kto napisał Pana Tadeusza? Adam Mickiewicz napisał "Pana Tadeusza". >>> ``` -------------------------------- ### Few-Shot Prompt Formatting Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Data_Generation_and_Fewshot_Prompting_(4_bit).ipynb Initializes a string variable for few-shot prompting, likely to be populated with examples. ```python few_shot_text = f""" ``` -------------------------------- ### Interactive LLM Session with Bielik Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_Ollama_integration.ipynb An example of an interactive session with the Bielik LLM after it has been run using Ollama. Demonstrates asking questions and receiving responses. ```shell (base) marcinwatroba@DataScienceServer:~$ ollama run SpeakLeash/bielik-11b-v2.2-instruct:Q4_K_M >>> Ile to jest 2+4? 2 plus 4 równa się 6. >>> Kto napisał Pana Tadeusza? Adam Mickiewicz napisał "Pana Tadeusza". >>> ``` -------------------------------- ### Run Ollama server in background (Colab) Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_Ollama_integration.ipynb Starts the Ollama server in the background within a Google Colab environment. Logs are redirected to 'rocama.log'. ```python !ollama serve > rocama.log 2>&1 & ``` -------------------------------- ### Run Docker Compose and Pull Bielik Model Source: https://context7.com/speakleash/bielik-how-to-start/llms.txt Commands to start the Docker environment and pull the specific Bielik model into the Ollama container. These are essential steps before running the provided notebooks. ```bash # Uruchomienie środowiska docker compose up # Pobranie modelu Bielik wewnątrz kontenera Ollama docker exec -i generative_ollama ollama pull SpeakLeash/bielik-7b-instruct-v0.1-gguf # Następnie uruchomić notebooki: # 0-import.ipynb – import i wektoryzacja danych # 1-rag.ipynb – odpytywanie danych z użyciem Bielika ``` -------------------------------- ### Test Text Generation with System Prompt Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_JSON.ipynb Example of calling the `generate` function with a system message defining the AI's persona and a user prompt. Adjust `temperature` and `max_tokens` as needed. ```python # Test z promptem systemowym i poleceniem od użytkownika temperature = 0.1 max_tokens = 1000 generate(prompt="Hej, powiedz mi czym się zajmujesz!", system="Jesteś programistą baz danych, który chce rozwijać swoje kompetencje w obszarze AI", temperature=temperature, max_tokens=max_tokens) ``` -------------------------------- ### Initialize Bielik-7B-Instruct Model and Tokenizer Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Instruct_QUANT_Tests.ipynb Loads the Bielik-7B-Instruct model and its corresponding tokenizer using the ctransformers and transformers libraries. It specifies the model file, quantization type, model type, and GPU layers for acceleration. Ensure you have the necessary libraries installed. ```python from ctransformers import AutoModelForCausalLM from transformers import AutoTokenizer, pipeline model = AutoModelForCausalLM.from_pretrained( "speakleash/Bielik-7B-Instruct-v0.1-GGUF", model_file="bielik-7b-instruct-v0.1.Q4_K_M.gguf", # różne typy kwantyzacji można pobrać z repozytorium speakleash/Bielik-7B-Instruct-v0.1-GGUF model_type="mistral", gpu_layers=50, hf=True ) tokenizer = AutoTokenizer.from_pretrained( "speakleash/Bielik-7B-Instruct-v0.1", use_fast=True ) pipe = pipeline(model=model, tokenizer=tokenizer, task='text-generation') ``` -------------------------------- ### Run Streamlit App and Expose via Localtunnel Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Streamlit_simple_app_tunnel_4bit.ipynb Starts the Streamlit application in the background and uses localtunnel to create a public URL for accessing the app. The output of the Streamlit process is redirected to a log file. ```bash # Po uruchomieniu będzie adres IP - należy go skopiować i przejść na stronę, która będzie poniżej !streamlit run app.py &>/content/logs.txt & !npx localtunnel --port 8501 & curl ipv4.icanhazip.com ``` -------------------------------- ### Extract and Structure Article Data to JSON Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_JSON.ipynb This example shows how to fetch an article from a URL and use a system prompt to instruct the model to extract and structure the article's data into a JSON object. The `pad_token_id` is set to `eos_token_id` for open-end generation. ```python article = get_article_from_url(url) system = 'Jesteś ekspertem od ekstrakcji danych z artykułów i strukturyzowania ich do formatu obiektów JSON. Przeanalizuj otrzymany tekst i zwróć odpowiedź z formacie JSON.' prompt = f'Tekst do analizy: {article}' max_tokens = 1000 generate(prompt=prompt, system=system, max_tokens=max_tokens) ``` -------------------------------- ### Integrate Bielik with Ollama for Local API Access Source: https://context7.com/speakleash/bielik-how-to-start/llms.txt Run Bielik as a local REST server using Ollama. This simplifies model management and allows interaction via standard HTTP API calls. Ensure Ollama is installed and the model is pulled. ```bash # Pobranie modelu Bielik V2 ollama pull SpeakLeash/bielik-11b-v2.2-instruct:Q4_K_M # Pobranie modelu Bielik V1 (GGUF Q4) ollama pull SpeakLeash/bielik-7b-instruct-v0.1-gguf:Q4_K_S # Odpytanie przez API REST (curl) curl http://localhost:11434/api/chat -d '{ "model": "SpeakLeash/bielik-11b-v2.2-instruct:Q4_K_M", "messages": [ {"role": "system", "content": "Odpowiadaj zawsze po polsku."}, {"role": "user", "content": "Wyjaśnij czym jest uczenie maszynowe."} ], "stream": false }' ``` -------------------------------- ### Prepare Chat Prompt for Unstructured JSON Output Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(AWQ)_structured_output.ipynb Creates a chat prompt with a detailed system message and an example to guide the AI model to extract characters and their equipment into a JSON format without a strict schema. ```python chat_unstructured = [ {"role": "system", "content": """ # ZADANIE: Z danego tekstu wybierz postacie i ich ekwipunek. ## Przykład: ### Tekst Bilbo miał dwa jabłka a Frodo miecz. ### Odpowiedź ```json { "characters": [ { "name": "Bilbo", "items": [ { "name": "jabłko", "quantity": 2 } ] }, { "name": "Frodo", "items": [ { "name": "miecz", "quantity": 1 } ] }, ] } ``` Odpowiadaj tylko w formacie JSON, nie dodawaj nic więcej."""}, {"role": "user", "content": f"# TEKST: { text }"}, ] prompt_unstructured = f"{tokenizer.apply_chat_template(chat_unstructured, tokenize=False)}<|im_start|>assistant # JSON:" ``` -------------------------------- ### Prepare for LLM Prompting Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_JSON.ipynb Loads the extracted article content and sets up system and user prompts for a language model. It also configures model parameters like temperature and max_tokens for controlled output. ```python # Pobranie treści artykułu do zmiennej article = get_article_from_url(url) # Przygotowanie promptów system = 'Jesteś ekspertem od ekstrakcji danych z tekstów i strukturyzowania ich do formatu obiektów JSON. Przeanalizuj otrzymany tekst i zwróć odpowiedź z formacie JSON z istotnymi według Ciebie kluczami.' prompt = f"Tekst do analizy: {article}" # Zwiększenie limitu tokenów i obniżenie temperatury (model mniej improwizuje) temperature = 0.0 max_tokens = 1000 ``` -------------------------------- ### Set Up Python Virtual Environment Source: https://github.com/speakleash/bielik-how-to-start/blob/main/weaviate/README.md Optional: Create and activate a dedicated Python virtual environment for your project to manage dependencies. ```sh python3 -m venv .venv source .venv/bin/activate ``` -------------------------------- ### Initialize LLM with AWQ Quantization Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Instruct_QUANT_Tests.ipynb Initializes the LLM with the 'speakleash/Bielik-7B-Instruct-v0.1-AWQ' model using AWQ quantization for memory efficiency. Ensure your environment has sufficient GPU memory. ```python from vllm import LLM, SamplingParams sampling_params = SamplingParams(temperature=0.0, top_p=1.0, max_tokens=256) llm = LLM( model="speakleash/Bielik-7B-Instruct-v0.1-AWQ", quantization='awq', dtype='half', gpu_memory_utilization=.95, max_model_len=4096 ) ``` -------------------------------- ### Bielik JSON Output Example Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_JSON.ipynb This is an example of the JSON output generated by Bielik, containing structured weather and forecast information. ```json { "dzień_tygodnia": "niedziela", "data": "2023-08-20", "temperatura": { "maksymalna": "34°C", "lokalizacje": ["Lublin", "Rzeszów", "Warszawa"] }, "zagrożenia": { "pożarowe": "ekstremalne, bardzo wysokie oraz wysokie", "susza": "36 alertów o suszy hydrologicznej" }, "prognoza_na_kolejne_dni": "30-stopniowe upały potrwają do końca przyszłego tygodnia, początek września ma przynieść niewielkie ochłodzenie" } ``` -------------------------------- ### Initialize Language Model and Tokenizer Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_RAG.ipynb Loads the Bielik-7B-Instruct-v0.1 model and its tokenizer using 4-bit quantization for efficiency. Sets up the tokenizer to use the end-of-sequence token for padding. ```python import torch from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextStreamer device = "cuda" model_name = 'speakleash/Bielik-7B-Instruct-v0.1' temperature = 1.0 max_tokens = 500 top_k = 200 top_p = 1 tokenizer = AutoTokenizer.from_pretrained(model_name) tokenizer.pad_token = tokenizer.eos_token streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True) quantization_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16 ) model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, quantization_config=quantization_config ) ``` -------------------------------- ### Run Bielik LLM via Ollama CLI Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Ollama_integration.ipynb Command to download and run the SpeakLeash/bielik-7b-instruct-v0.1-gguf model using Ollama. The model will be downloaded on the first run. ```shell ollama run SpeakLeash/bielik-7b-instruct-v0.1-gguf ``` -------------------------------- ### Initialize ChromaDB Client and Collection Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_RAG.ipynb Sets up a persistent ChromaDB client and creates or retrieves a collection for storing document embeddings. The collection is configured with cosine similarity for searching. ```python import chromadb client = chromadb.PersistentClient(path=data_dir) collection = client.get_or_create_collection( name="ha_naive_rag", metadata={"hnsw:space": "cosine"} ) ``` -------------------------------- ### Run Contract Enhancer Application Source: https://github.com/speakleash/bielik-how-to-start/blob/main/contract_enhancer/README.md Launches the Contract Enhancer application. Follow the on-screen instructions in the user interface to correct documents. ```bash python interface.py ``` -------------------------------- ### Set up Weaviate and Ollama with Docker Compose Source: https://context7.com/speakleash/bielik-how-to-start/llms.txt Configure and launch Weaviate and Ollama services using Docker Compose. Ensure Ollama is running to pull the Bielik model. ```yaml services: weaviate: image: cr.weaviate.io/semitechnologies/weaviate:1.27.6 ports: ["8080:8080", "50051:50051"] environment: ENABLE_MODULES: "text2vec-transformers,generative-ollama" TRANSFORMERS_INFERENCE_API: "http://t2v-transformers-baai-bge-m3-onnx:8080" DEFAULT_VECTORIZER_MODULE: "none" t2v-transformers-baai-bge-m3-onnx: image: cr.weaviate.io/semitechnologies/transformers-inference:baai-bge-m3-onnx generative-ollama: image: ollama/ollama:0.3.14 container_name: generative_ollama volumes: ["./.data/generative_ollama:/root/.ollama"] ``` -------------------------------- ### Configure and Load Bielik Model with 4-bit Quantization Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_Text_Improvement.ipynb Sets up model parameters, including device, name, and generation settings. It then loads the specified Bielik model using 4-bit quantization for memory efficiency, setting the pad token and configuring the model for causal language modeling. ```python device = "cuda" model_name = "speakleash/Bielik-11B-v2.2-Instruct" max_tokens = 5000 temperature = 0 top_k = 0 top_p = 0 tokenizer = AutoTokenizer.from_pretrained(model_name) tokenizer.pad_token = tokenizer.eos_token streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True) quantization_config = BitsAndBytesConfig( load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16 ) model = AutoModelForCausalLM.from_pretrained(model_name, torch_dtype=torch.bfloat16, quantization_config=quantization_config, low_cpu_mem_usage = True ) model.generation_config.pad_token_id = tokenizer.pad_token_id ``` -------------------------------- ### Connect to Local Weaviate Instance Source: https://github.com/speakleash/bielik-how-to-start/blob/main/weaviate/notebooks/1-rag.ipynb Establishes a connection to a local Weaviate instance with custom timeouts for initialization, queries, and insertions. Requires the 'weaviate' library. ```python import weaviate from weaviate.classes.init import AdditionalConfig, Timeout client = weaviate.connect_to_local(additional_config=AdditionalConfig( timeout=Timeout(init=30, query=4800, insert=120) # Values in seconds )) collection = client.collections.get("Articles") ``` -------------------------------- ### Generate Response with System Prompt Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Data_Generation_and_Fewshot_Prompting_(4_bit).ipynb Generates a response using a predefined system prompt that guides the chatbot's behavior and output format. ```python system_prompt = """Jesteś chatbotem odpowiadającym zwięźle na pytania na podstawie podanego kontekstu. Niektóre pytania mogą być niemożliwe, w takim wypadku musisz wstrzymać się od odpowiedzi. Twoje odpowiedzi mają przyjąć formę: "Odpowiedź: 'treść odpowiedzi'" jeśli treść odpowiedź istnieje w tekście lub "Odpowiedź niemożliwa!" jeśli nie da się odpowiedzieć na pytanie na podstawie kontekstu.""" output = generate(prompt=prompt_possible, system=system_prompt) ``` -------------------------------- ### Extract AI Workshops in JSON Format Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_simple_examples.ipynb Extracts AI workshop information and formats it as a JSON array. This example specifies a JSON template for the output. ```python system = """Wymień warsztaty dotyczące AI z poniższego tekstu strony internetowej. Zwróć w postaci JSON według szablonu [ { 'trener':..., 'temat':.., 'godzina': ... } , ``` -------------------------------- ### RAG Model Configuration Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_OlimpiadaAI_RAG.ipynb Sets up essential parameters for the RAG system, including ChromaDB path, model details, and generation temperature. Ensure LM Studio is running for model access. ```python CHROMA_PATH = "data/chromadb" # ścieżka do zapisu bazy wektorowej na dysku MODEL_NAME = "bielik-11b-v2.2-instruct@q8_0" # z LM Studio MODEL_URL = "http://127.0.0.1:1234/v1" # z LM Studio MODEL_API_KEY = "not-needed" # z LM Studio TEMPERATURE = 0.0 # im niższa, tym model bardziej analityczny, im wyższa tym bardziej kreatywny (zakres 0-1) ``` -------------------------------- ### Connect to Local Weaviate Instance Source: https://github.com/speakleash/bielik-how-to-start/blob/main/weaviate/notebooks/0-import.ipynb Establishes a connection to a Weaviate instance running locally. Ensure Weaviate is running before executing this. ```python client = weaviate.connect_to_local() ``` -------------------------------- ### Ollama API Chat Request with Curl Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Ollama_integration.ipynb Example of sending a chat request to the Ollama API using curl. This demonstrates how to interact with the model programmatically. ```shell curl http://localhost:11434/api/chat -d '{ "model": "SpeakLeash/bielik-7b-instruct-v0.1-gguf", "stream": false, "messages": [ { "role": "system", "content": "Odpowiedz krótko na pytanie" }, { "role": "user", "content": "Kim jest Adam Mickiewicz?" } ] }' ``` -------------------------------- ### Implement RAG with ChromaDB and Bielik Source: https://context7.com/speakleash/bielik-how-to-start/llms.txt Set up a question-answering system using Retrieval-Augmented Generation. This involves parsing PDFs, creating embeddings, storing them in ChromaDB, and using Bielik for context-aware responses. Requires `fitz`, `chromadb`, `sentence-transformers`, and `transformers` libraries. ```python import fitz, json, requests, chromadb, torch from sentence_transformers import SentenceTransformer from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig, TextStreamer # 1. Pobieranie i parsowanie PDF pdf_url = "https://www.iab.org.pl/wp-content/uploads/2024/04/Przewodnik-po-sztucznej-inteligencji-2024_IAB-Polska.pdf" with open("dokument.pdf", "wb") as f: f.write(requests.get(pdf_url, stream=True).content) document = fitz.open("dokument.pdf") pages = [{"page_num": i, "text": page.get_text()} for i, page in enumerate(document)] texts = [p["text"] for p in pages] # 2. Tworzenie embeddingów i bazy wektorowej embedding_model = SentenceTransformer("ipipan/silver-retriever-base-v1.1") embeddings = embedding_model.encode(texts) client = chromadb.PersistentClient(path="./data") collection = client.get_or_create_collection("bielik_rag", metadata={"hnsw:space": "cosine"}) collection.add(documents=texts, embeddings=embeddings, ids=[str(i) for i in range(len(texts))]) # 3. Funkcja tworzenia kontekstu z wyszukiwania wektorowego def create_context(query: str, top: int = 3) -> str: embedding = embedding_model.encode([query]) result = collection.query(query_embeddings=embedding, n_results=top) docs = result.get("documents", [[]])[0] context = "KONTEKST:\n" + "\n\n".join(docs) return f"Wyłącznie na podstawie podanego kontekstu odpowiedz zwięźle na pytanie: '{query}'\n{context}" # 4. Inicjalizacja modelu i odpowiedź model_name = "speakleash/Bielik-11B-v2.2-Instruct" tokenizer = AutoTokenizer.from_pretrained(model_name) tokenizer.pad_token = tokenizer.eos_token model = AutoModelForCausalLM.from_pretrained( model_name, torch_dtype=torch.bfloat16, quantization_config=BitsAndBytesConfig(load_in_4bit=True, bnb_4bit_compute_dtype=torch.bfloat16) ) streamer = TextStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True) prompt = create_context("Czy AI zniszczy ludzkość?", top=3) inputs = tokenizer.apply_chat_template([{"role": "user", "content": prompt}], return_tensors="pt").to("cuda") model.generate(inputs, streamer=streamer, max_new_tokens=500, temperature=0.1, do_sample=True) ``` -------------------------------- ### Analyze Sentiment with Prompt Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_simple_examples.ipynb Analyze the sentiment of a given review and provide a detailed explanation. The prompt guides the model to classify sentiment and justify the reasoning. ```python review = "Przy waszych odsetkach to tylko chleb i margaryna!" prompt = f"Czy podana opinia jest pozytywna czy negatywna i wyjaśnij to obszernie.\n\n###OPINIA:\n{review}" generate(prompt) ``` -------------------------------- ### Generate JSON Schema from Pydantic Model Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(AWQ)_structured_output.ipynb Generates a JSON schema from a Pydantic model, which can be used to guide AI models in producing structured output. ```python schema = Company.model_json_schema() ``` -------------------------------- ### Test Function to Get Answer with Context Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_OlimpiadaAI_RAG.ipynb Tests the `get_answer_with_context` function with a sample query and a placeholder for context. Assumes `client` and `TEMPERATURE` are already defined. ```python # Test funkcjonalności get_answer_with_context('jakie są najmodniejsze kolory włosów?', context) ``` -------------------------------- ### Clone Repository using Git Source: https://github.com/speakleash/bielik-how-to-start/blob/main/README.md Use this command to download the project files to your local machine. ```bash git clone https://github.com/speakleash/Bielik-how-to-start.git ``` -------------------------------- ### Define Ollama API Constants Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_Ollama_integration.ipynb Sets up the URL and payload for interacting with the Ollama chat API. Ensure the model name matches your local Ollama setup. ```python URL = "http://localhost:11434/api/chat" PAYLOAD = { "model": "SpeakLeash/bielik-11b-v2.2-instruct:Q4_K_M", "stream": False, "messages": [ { "role": "system", "content": "Odpowiedz krótko na pytanie" }, { "role": "user", "content": "Kim jest Adam Mickiewicz?" } ] } ``` -------------------------------- ### Fetch Web Page Content Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_simple_examples.ipynb Fetches and extracts plain text content from a given URL using requests and BeautifulSoup. Ensure the 'requests' and 'beautifulsoup4' libraries are installed. ```python import requests from bs4 import BeautifulSoup def get_page_content(url='https://hiperautomatyzacja.pl'): response = requests.get(url) html_content = response.text soup = BeautifulSoup(html_content, 'html.parser') plain_text = soup.get_text() clean_text = plain_text return clean_text ``` -------------------------------- ### Get Context from Vector Database Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_OlimpiadaAI_RAG.ipynb Encodes a user query, searches a vector database for the most relevant documents, and returns them as a formatted string. Requires an initialized embedding model and a 'collection' object. ```python def get_context(query: str, top_results: int=3) -> str: """ Metoda pozwalająca na wyszukiwanie interesującego nas kontekstu w wektorowej bazie wiedzy. """ query_embedding = embedding_model.encode(query) results = collection.query( query_embeddings=query_embedding, n_results=top_results, ) if results: documents = results.get('documents', [])[0] context = "### ".join(documents) return context else: print('Nie znaleziono kontekstu') return "" ``` -------------------------------- ### Prepare Prompt for GPTQ Model Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_Instruct_QUANT_Tests.ipynb Formats a human prompt into the specific instruction format required by the model, including the `[INST]` tags. ```python human_prompt = "Wymień trzy ciekawe miejscowości w Polsce i krótko je scharakteryzuj?" prompt = f"[INST]{human_prompt}[/INST]" print(prompt) ``` -------------------------------- ### Generate Response with System Prompt Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_(4_bit)_simple_examples.ipynb Generates a response to a user prompt, incorporating a system-level instruction to define the AI's persona. This example sets the AI as a database programmer interested in AI. ```python generate("Hej, czym się zajmujesz?", system = "Jesteś programistą baz danych, który chce rozwijać swoje kompetencje w obszarze AI.") ``` -------------------------------- ### Create and Populate ChromaDB Collection Source: https://github.com/speakleash/bielik-how-to-start/blob/main/Bielik_2_(4_bit)_RAG.ipynb Initializes a persistent ChromaDB client, creates a collection named 'ha_naive_rag' with cosine similarity, and adds documents with their embeddings and IDs. Ensure 'texts' and 'embeddings' are pre-defined variables. ```python import chromadb client = chromadb.PersistentClient(path=data_dir) collection = client.get_or_create_collection( name="ha_naive_rag", metadata={"hnsw:space": "cosine"} ) collection.add( documents=texts, embeddings=embeddings, ids=[str(i) for i in range(len(texts))] ) ```