RagModel tracked in Weave, evaluate the updated application with weave.Evaluation, and publish the new RAG model back to the Registry. This workflow is intended for teams who train and fine-tune models with W&B Models and want to integrate them into LLM applications tracked and evaluated with Weave.
This is an interactive notebook. You can run it locally or use the following links:
Prerequisites
First, install the required libraries, set up API keys, log in to W&B, and create a new W&B project.- Install
weave,pandas,unsloth,wandb,litellm,pydantic,torch, andfaiss-gpuusingpip.
- Add the required API keys from your environment.
- Log in to W&B, and create a new project.
Download ChatModel from Models Registry and implement UnslothLoRAChatModel
In this scenario, the Model Team has already fine-tuned the Llama-3.2 model with the unsloth library for performance optimization, and the model is available in the W&B Models Registry. In this step, you retrieve the fine-tuned ChatModel from the Registry and convert it into a weave.Model to make it compatible with the RagModel.
The
RagModel referenced in the following code is a top-level weave.Model that can be considered a complete RAG Application. It contains a ChatModel, vector database, and a prompt. The ChatModel is also a weave.Model, which contains code to download an artifact from the W&B Registry. ChatModel can be changed modularly to support any kind of other LLM chat model as part of the RagModel. For more information, view the model in Weave.ChatModel, use unsloth.FastLanguageModel or peft.AutoPeftModelForCausalLM with adapters for efficient integration into the app. After you download the model from the Registry, set up the initialization and prediction logic with the model_post_init method. The required code for this step is available in the Use tab of the Registry, and you can copy it directly into your implementation.
The following code defines the UnslothLoRAChatModel class to manage, initialize, and use the fine-tuned Llama-3.2 model retrieved from the W&B Models Registry. UnslothLoRAChatModel uses unsloth.FastLanguageModel for optimized inference. The model_post_init method downloads and sets up the model, while the predict method processes user queries and generates responses. To adapt the code for your use case, update the MODEL_REG_URL with the correct Registry path for your fine-tuned model, and adjust parameters like max_seq_length or dtype based on your hardware or requirements.
Integrate the new ChatModel version into RagModel
Building a RAG application from a fine-tuned chat model lets you reuse tailored components without rebuilding the entire pipeline. In this step, you retrieve the existing RagModel from your Weave project and update its ChatModel to use the fine-tuned model. Swapping in the new chat model leaves the other components, like the vector database and prompts, untouched, preserving the application’s overall structure while improving performance.
The following code retrieves the RagModel object using a reference from the Weave project. The code then updates the chat_model attribute of the RagModel to use the new UnslothLoRAChatModel instance created in the previous step. After this, the code publishes the updated RagModel to create a new version. Finally, the code runs a sample prediction query with the updated RagModel to verify that it uses the new chat model.
Run a weave.Evaluation
With the updated RagModel published, the next step is to confirm that the new fine-tuned chat model performs as expected within the application. In this step, you evaluate the performance of the updated RagModel with an existing weave.Evaluation. This process confirms that the new fine-tuned chat model performs as expected within the RAG application. To streamline integration and enable collaboration between the Models and Apps teams, you log evaluation results to both the model’s W&B run and the Weave workspace.
In Models:
- The evaluation summary is logged to the W&B run used to download the fine-tuned chat model. This includes summary metrics and graphs displayed in a workspace view for analysis.
- The evaluation trace ID is added to the run’s configuration, linking directly to the Weave page for improved traceability by the Model Team.
- The artifact or registry link for the
ChatModelis stored as an input to theRagModel. - The W&B run ID is saved as an extra column in the evaluation traces for better context.
RagModel, and log the results to both W&B and Weave. Ensure that the evaluation reference (WEAVE_EVAL) matches your project setup.
Save the new RAG model to the Registry
Now that you’ve evaluated the updatedRagModel, the final step is to publish it back to the W&B Models Registry so other teams can discover and reuse it. To make the updated RagModel available for future use by both the Models and Apps teams, push it to the W&B Models Registry as a reference artifact.
The following code retrieves the weave object version and name for the updated RagModel and uses them to create reference links. The code then creates a new artifact in W&B with metadata containing the model’s Weave URL. The code logs this artifact to the W&B Registry and links it to a designated registry path.
Before running the code, ensure the ENTITY and PROJECT variables match your W&B setup, and specify the correct target registry path. This process finalizes the workflow by publishing the new RagModel to the W&B ecosystem for collaboration and reuse.
After running the code in this section, your updated RagModel is available in the W&B Models Registry as a referenced artifact, completing the round-trip between W&B Models and Weave.