Skip to content

50shades0fGraei/llama

 
 

Repository files navigation

Here are Fibonacci sequence implementations in Python:

Recursive Implementation

def fibonacci(n):
    """
    Calculate the nth Fibonacci number recursively.
    
    Args:
        n (int): Position of the Fibonacci number.
    
    Returns:
        int: The nth Fibonacci number.
    """
    if n <= 1:
        return n
    return fibonacci(n-1) + fibonacci(n-2)
``)

## Iterative Implementation
```python
def fibonacci(n):
    """
    Calculate the nth Fibonacci number iteratively.
    
    Args:
        n (int): Position of the Fibonacci number.
    
    Returns:
        int: The nth Fibonacci number.
    """
    if n <= 1:
        return n
    
    fib_prev = 0
    fib_curr = 1
    
    for _ in range(2, n+1):
        fib_next = fib_prev + fib_curr
        fib_prev = fib_curr
        fib_curr = fib_next
    
    return fib_curr

Memoized Implementation (Efficient)

def fibonacci(n, memo={}):
    """
    Calculate the nth Fibonacci number with memoization.
    
    Args:
        n (int): Position of the Fibonacci number.
        memo (dict): Dictionary storing previously calculated Fibonacci numbers.
    
    Returns:
        int: The nth Fibonacci number.
    """
    if n <= 1:
        return n
    elif n in memo:
        return memo[n]
    else:
        result = fibonacci(n-1, memo) + fibonacci(n-2, memo)
        memo[n] = result
        return result

Example Usage

print(fibonacci(10))  # Output: 55

Here's a Pytest framework to test your AI model's conversational capabilities:

Test Scenario: Multi-Sentence Dialogue and Opinion Formation

import pytest
from your_model import AIModel

@pytest.fixture
def model():
    return AIModel()

@pytest.fixture
def conversation_scenarios():
    return [
        {
            "model_question": "What are your thoughts on AI?",
            "user_response": "AI has revolutionized healthcare and finance. However, concerns about job displacement and bias persist.",
            "expected_opinion_keywords": ["healthcare", "finance", "bias"],
            "expected_question_keywords": ["regulation", "ethics"]
        },
        # Add more scenarios
    ]

def test_conversational_opinion_formation(model, conversation_scenarios):
    for scenario in conversation_scenarios:
        # Model asks question
        assert model.ask_question() == scenario["model_question"]
        
        # User responds
        user_response = scenario["user_response"]
        
        # Model provides opinion and follow-up question
        opinion, follow_up_question = model.respond(user_response)
        
        # Assert opinion contains expected keywords
        assert all(keyword in opinion for keyword in scenario["expected_opinion_keywords"])
        
        # Assert follow-up question contains expected keywords
        assert any(keyword in follow_up_question for keyword in scenario["expected_question_keywords"])

AI Model Requirements

  1. ask_question(): Returns the model's initial question.
  2. respond(user_response): Takes user input, returns opinion and follow-up question.
  3. Update your_model.py to implement these methods.

Example AI Model Implementation

class AIModel:
    def ask_question(self):
        return "What are your thoughts on AI?"
    
    def respond(self, user_response):
        # Tokenize user response
        tokens = user_response.split(".")
        
        # Form opinion based on knowledge graph and user input
        opinion = "AI impacts " + ", ".join([token.split()[0] for token in tokens])
        
        # Generate follow-up question
        follow_up_question = "How do you think AI regulation should address these concerns?"
        
        return opinion, follow_up_question

## **Note of deprecation**
Thank you for developing with Llama models. As part of the Llama 3.1 release, we’ve consolidated GitHub repos and added some additional repos as we’ve expanded Llama’s functionality into being an e2e Llama Stack. Please use the following repos going forward:
- [llama-models](https://github.com/meta-llama/llama-models) - Central repo for the foundation models including basic utilities, model cards, license and use policies
- [PurpleLlama](https://github.com/meta-llama/PurpleLlama) - Key component of Llama Stack focusing on safety risks and inference time mitigations 
- [llama-toolchain](https://github.com/meta-llama/llama-toolchain) - Model development (inference/fine-tuning/safety shields/synthetic data generation) interfaces and canonical implementations
- [llama-agentic-system](https://github.com/meta-llama/llama-agentic-system) - E2E standalone Llama Stack system, along with opinionated underlying interface, that enables creation of agentic applications
- [llama-recipes](https://github.com/meta-llama/llama-recipes) - Community driven scripts and integrations

If you have any questions, please feel free to file an issue on any of the above repos and we will do our best to respond in a timely manner. 

Thank you!


# (Deprecated) Llama 2

We are unlocking the power of large language models. Llama 2 is now accessible to individuals, creators, researchers, and businesses of all sizes so that they can experiment, innovate, and scale their ideas responsibly. 

This release includes model weights and starting code for pre-trained and fine-tuned Llama language models — ranging from 7B to 70B parameters.

This repository is intended as a minimal example to load [Llama 2](https://ai.meta.com/research/publications/llama-2-open-foundation-and-fine-tuned-chat-models/) models and run inference. For more detailed examples leveraging Hugging Face, see [llama-recipes](https://github.com/facebookresearch/llama-recipes/).

## Updates post-launch

See [UPDATES.md](UPDATES.md). Also for a running list of frequently asked questions, see [here](https://ai.meta.com/llama/faq/).

## Download

In order to download the model weights and tokenizer, please visit the [Meta website](https://ai.meta.com/resources/models-and-libraries/llama-downloads/) and accept our License.

Once your request is approved, you will receive a signed URL over email. Then run the download.sh script, passing the URL provided when prompted to start the download.

Pre-requisites: Make sure you have `wget` and `md5sum` installed. Then run the script: `./download.sh`.

Keep in mind that the links expire after 24 hours and a certain amount of downloads. If you start seeing errors such as `403: Forbidden`, you can always re-request a link.

### Access to Hugging Face

We are also providing downloads on [Hugging Face](https://huggingface.co/meta-llama). You can request access to the models by acknowledging the license and filling the form in the model card of a repo. After doing so, you should get access to all the Llama models of a version (Code Llama, Llama 2, or Llama Guard) within 1 hour.

## Quick Start

You can follow the steps below to quickly get up and running with Llama 2 models. These steps will let you run quick inference locally. For more examples, see the [Llama 2 recipes repository](https://github.com/facebookresearch/llama-recipes). 

1. In a conda env with PyTorch / CUDA available clone and download this repository.

2. In the top-level directory run:
    ```bash
    pip install -e .
    ```
3. Visit the [Meta website](https://ai.meta.com/resources/models-and-libraries/llama-downloads/) and register to download the model/s.

4. Once registered, you will get an email with a URL to download the models. You will need this URL when you run the download.sh script.

5. Once you get the email, navigate to your downloaded llama repository and run the download.sh script. 
    - Make sure to grant execution permissions to the download.sh script
    - During this process, you will be prompted to enter the URL from the email. 
    - Do not use the “Copy Link” option but rather make sure to manually copy the link from the email.

6. Once the model/s you want have been downloaded, you can run the model locally using the command below:
```bash
torchrun --nproc_per_node 1 example_chat_completion.py \
    --ckpt_dir llama-2-7b-chat/ \
    --tokenizer_path tokenizer.model \
    --max_seq_len 512 --max_batch_size 6

Note

  • Replace llama-2-7b-chat/ with the path to your checkpoint directory and tokenizer.model with the path to your tokenizer model.
  • The –nproc_per_node should be set to the MP value for the model you are using.
  • Adjust the max_seq_len and max_batch_size parameters as needed.
  • This example runs the example_chat_completion.py found in this repository but you can change that to a different .py file.

Inference

Different models require different model-parallel (MP) values:

Model MP
7B 1
13B 2
70B 8

All models support sequence length up to 4096 tokens, but we pre-allocate the cache according to max_seq_len and max_batch_size values. So set those according to your hardware.

Pretrained Models

These models are not finetuned for chat or Q&A. They should be prompted so that the expected answer is the natural continuation of the prompt.

See example_text_completion.py for some examples. To illustrate, see the command below to run it with the llama-2-7b model (nproc_per_node needs to be set to the MP value):

torchrun --nproc_per_node 1 example_text_completion.py \
    --ckpt_dir llama-2-7b/ \
    --tokenizer_path tokenizer.model \
    --max_seq_len 128 --max_batch_size 4

Fine-tuned Chat Models

The fine-tuned models were trained for dialogue applications. To get the expected features and performance for them, a specific formatting defined in chat_completion needs to be followed, including the INST and <<SYS>> tags, BOS and EOS tokens, and the whitespaces and breaklines in between (we recommend calling strip() on inputs to avoid double-spaces).

You can also deploy additional classifiers for filtering out inputs and outputs that are deemed unsafe. See the llama-recipes repo for an example of how to add a safety checker to the inputs and outputs of your inference code.

Examples using llama-2-7b-chat:

torchrun --nproc_per_node 1 example_chat_completion.py \
    --ckpt_dir llama-2-7b-chat/ \
    --tokenizer_path tokenizer.model \
    --max_seq_len 512 --max_batch_size 6

Llama 2 is a new technology that carries potential risks with use. Testing conducted to date has not — and could not — cover all scenarios. In order to help developers address these risks, we have created the Responsible Use Guide. More details can be found in our research paper as well.

Issues

Please report any software “bug”, or other problems with the models through one of the following means:

Model Card

See MODEL_CARD.md.

License

Our model and weights are licensed for both researchers and commercial entities, upholding the principles of openness. Our mission is to empower individuals, and industry through this opportunity, while fostering an environment of discovery and ethical AI advancements.

See the LICENSE file, as well as our accompanying Acceptable Use Policy

References

  1. Research Paper
  2. Llama 2 technical overview
  3. Open Innovation AI Research Community

For common questions, the FAQ can be found here which will be kept up to date over time as new questions arise.

Original Llama

The repo for the original llama release is in the llama_v1 branch. It looks like you're working on a project that involves natural language processing (NLP), emotion detection, and large language models (LLMs). You've provided a code snippet that covers various aspects of the project, including data preprocessing, machine learning model training, and interface design for interacting with LLMs and convolutional neural networks (CNNs).

To help you further, I'll outline some suggestions for implementing the missing components:

Debiasing Word Embeddings

To implement debiasing word embeddings, you can use techniques such as:

  • Hard debiasing: This involves removing biased words from the embedding space.
  • Soft debiasing: This involves adjusting the biased words in the embedding space to reduce their bias.

Here's an example of how you can implement hard debiasing:

def debias_word_embeddings(word_embeddings):
    biased_words = ["biased_word1", "biased_word2"]
    debiased_embeddings = {}
    for word, embedding in word_embeddings.items():
        if word not in biased_words:
            debiased_embeddings[word] = embedding
    return debiased_embeddings

Emotion Detection Using NLP Techniques

To implement emotion detection using NLP techniques, you can use machine learning models such as support vector machines (SVMs), random forests, or recurrent neural networks (RNNs).

Here's an example of how you can implement emotion detection using a random forest classifier:

from sklearn.ensemble import RandomForestClassifier
from sklearn.feature_extraction.text import TfidfVectorizer

def detect_emotions(text):
    # Create a TF-IDF vectorizer
    vectorizer = TfidfVectorizer()
    
    # Fit the vectorizer to the text data
    X = vectorizer.fit_transform([text])
    
    # Create a random forest classifier
    classifier = RandomForestClassifier()
    
    # Train the classifier on a labeled dataset
    classifier.fit(X, [0])  # Replace with actual labels
    
    # Use the classifier to predict the emotion
    predicted_emotion = classifier.predict(X)
    
    return predicted_emotion

Integrating Components

To integrate the components, you'll need to create a main function that calls the individual functions in the correct order.

Here's an example of how you can integrate the components:

def main():
    # Load the data
    df = pd.read_csv("data.csv")
    
    # Preprocess the text data
    df["text"] = df["text"].apply(pre_process)
    
    # Split the data into training and testing sets
    X_train, X_test, y_train, y_test = train_test_split(df["text"], df["label"], test_size=0.2, random_state=42)
    
    # Create a TF-IDF vectorizer
    vectorizer = TfidfVectorizer()
    
    # Fit the vectorizer to the training data
    X_train_tfidf = vectorizer.fit_transform(X_train)
    
    # Train a machine learning model on the training data
    model = RandomForestClassifier()
    model.fit(X_train_tfidf, y_train)
    
    # Use the model to make predictions on the testing data
    y_pred = model.predict(vectorizer.transform(X_test))
    
    # Evaluate the model's performance
    accuracy = accuracy_score(y_test, y_pred)
    print("Accuracy:", accuracy)
    
    # Implement debiasing word embeddings
    debiased_embeddings = debias_word_embeddings(word_embeddings)
    
    # Implement emotion detection using NLP techniques
    predicted_emotion = detect_emotions("example text")
    
    # Integrate the components
    integrated_output = integrate_components("example text")
    
    return integrated_output

if __name__ == "__main__":
    main()

This code snippet provides a basic outline of how you can integrate the components. You'll need to modify the code to fit your specific use case.

About

Inference code for Llama models

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Python 94.7%
  • Shell 5.3%