Skip to content

timkitch/langchain-code-understanding

Repository files navigation

Langchain and Langsmith Code Example

This repository contains a Python script that integrates various Langchain and Langsmith tools to provide a robust question-answering system based on dynamic content retrieval from a specified Git repository. The code showcases how to clone a repository, parse documents, split texts, create a searchable database, and build a question-answering chain using different language models.

Features

  • Git Repository Cloning: Automatically clones a Git repository to a local directory for content analysis.
  • Document Loading and Parsing: Uses the GenericLoader and LanguageParser to load and parse documents from the cloned repository.
  • Text Splitting: Implements a RecursiveCharacterTextSplitter for dividing documents into manageable chunks.
  • Database and Retriever Creation: Utilizes Chroma and OpenAIEmbeddings to create a searchable database of text documents.
  • Question-Answering Chain: Builds a QA chain that integrates a retriever for fetching relevant content and a language model for generating answers.

Setup and Installation

  1. Create API keys for your desired LLM and also for Langsmith.: Create your LLM keys for the desired language model of your choice. Create your Langsmith API key by signing up for a free account at https://langsmith.com/signup.

  2. Set Environment Variables: Copy the .env_example file to a new file named .env and fill in the required environment variables. Comment/uncomment the llm model near the top of the cu.py file to use the llm model of your choice.

  3. Environment Setup: Make sure Python 3.10+ is installed. Run pip install -r requirements.txt to install the necessary packages.

Usage

  1. Start the Script: Run the script by executing python cu.py on the command line. You will be prompted to:

    • Enter the URL of the Git repository you want to chat with.
    • Optionally specify a branch name (press Enter for the default branch).
    • Choose whether to save the chat session to a file.
    • If saving, provide a filename for the chat log. The script will handle the cloning and loading processes based on your input.
  2. Question-Answering: Once the system is ready, you can start asking questions. The system will use the created QA chain to retrieve relevant information and provide answers. If you chose to save the chat, all questions and answers will be logged to the specified file.

  3. Feedback Loop: After receiving each answer, you can rate the response and provide comments for feedback. This is crucial for improving the system's accuracy and user experience.

  4. Session Saving: If you chose to save the chat session, the script will automatically append each question and answer to the specified file. At the end of the session, it will confirm that the chat has been saved and provide the full filename.

Command Line Interaction Example

(myenv) (base) timkitch@DESKTOP-9B877UH:~/ai-projects/langchain-code-understanding$ python cu.py

Enter repo URL: https://github.com/timkitch/blckly-api.git

Enter branch name (press Enter for default branch):

Local repo path already exists. Not cloning.

Do you want to save this chat to a file? (yes/no): yes Enter a filename to save the chat: example_chat Chat will be saved to: example_chat_2024-08-03_21-15-30.txt

User ID: 8aaf0682-9b3b-4773-ad51-3bd523dbf65d

Enter a question or task (or 'x' to exit): What car kit colors does this app allow me to choose when placing an order?

Langsmith run id: 7c86220f-74cb-40ed-b300-161269fd6bb6

Answer: Based on the code provided, the available car kit colors that the user can choose from are defined in the Color enum:

package com.blckly.model;

public enum Color {
    WHITE("White"), 
    RED("Red"), 
    YELLOW("Yellow"), 
    BLUE("Blue");

    private String color;

    Color(String color) {
        this.color = color;
    }

    public String getColor() {
        return color;
    }

    public static Color getDefault() {
        return WHITE;
    }
}

The available colors are:

  • White
  • Red
  • Yellow
  • Blue

The getDefault() method returns the default color, which is White.

Rate the response (1-5): 5

(Optional) Enter any comments for feedback: That's a perfect response!

Enter a question or task (or 'x' to exit): x

Chat saved to example_chat_2024-08-03_21-15-30.txt

Langsmith Traces

The below screenshot shows the Langsmith trace for the above question/answer chain execution.

Langsmith

This Langsmith screenshot shows the rating and comments provided by the user.

Langsmith

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages