teleren/devto huggingface dataset:
RAG with Ollama + Mistral + Llama Index
Published: 2024-01-28 07:31:49

**RAG with Ollama + Mistral + Llama Index**



pip install llama-index qdrant_client torch transformers

# Import modules
from llama_index.llms import Ollama
from pathlib import Path
import qdrant_client
from llama_index import VectorStoreIndex, ServiceContext, download_loader
from llama_index.storage.storage_context import StorageContext
from llama_index.vector_stores.qdrant import QdrantVectorStore

# Load JSON data
JSONReader = download_loader("JSONReader")
loader = JSONReader()
documents = loader.load_data(Path('tinytweets.json'))

# Create Qdrant client and store
client = qdrant_client.QdrantClient(path="./qdrant_data")
vector_store = QdrantVectorStore(client=client, collection_name="tweets")
storage_context = StorageContext.from_defaults(vector_store=vector_store)

# Initialize Ollama and ServiceContext
llm = Ollama(model="mistral")
service_context = ServiceContext.from_defaults(llm=llm, embed_model="local")

# Create VectorStoreIndex and query engine
index = VectorStoreIndex.from_documents(documents, service_context=service_context, storage_context=storage_context)
query_engine = index.as_query_engine()

# Perform a query and print the response
response = query_engine.query("What does the author think about Star Trek? In One line")
print(response)

# Import modules
import qdrant_client
from llama_index import VectorStoreIndex, ServiceContext
from llama_index.llms import Ollama
from llama_index.vector_stores.qdrant import QdrantVectorStore

# Create Qdrant client and vector store
client = qdrant_client.QdrantClient(path="./qdrant_data")
vector_store = QdrantVectorStore(client=client, collection_name="tweets")

# Initialize Ollama and ServiceContext
llm = Ollama(model="mistral")
service_context = ServiceContext.from_defaults(llm=llm, embed_model="local")

# Create VectorStoreIndex and query engine with a similarity threshold of 20
index = VectorStoreIndex.from_vector_store(vector_store=vector_store, service_context=service_context)
query_engine = index.as_query_engine(similarity_top_k=20)

# Perform a query and print the response
response = query_engine.query("What does the author think about Star Trek? In One line")
print(response)
RAG with ChromaDB + Llama Index + Ollama
Published: 2024-01-28 07:26:34

**RAG with ChromaDB + Llama Index + Ollama
**



pip install llama-index torch transformers chromadb

# Import modules
from llama_index.llms import Ollama
from pathlib import Path
import chromadb
from llama_index import VectorStoreIndex, ServiceContext, download_loader
from llama_index.storage.storage_context import StorageContext
from llama_index.vector_stores.chroma import ChromaVectorStore

# Load JSON data
JSONReader = download_loader("JSONReader")
loader = JSONReader()
documents = loader.load_data(Path('tinytweets.json'))

# Create Chroma DB client and store
client = chromadb.PersistentClient(path="./chroma_db_data")
chroma_collection = client.create_collection(name="tweets")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)
storage_context = StorageContext.from_defaults(vector_store=vector_store)

# Initialize Ollama and ServiceContext
llm = Ollama(model="mistral")
service_context = ServiceContext.from_defaults(llm=llm, embed_model="local")

# Create VectorStoreIndex and query engine
index = VectorStoreIndex.from_documents(documents, service_context=service_context, storage_context=storage_context)
query_engine = index.as_query_engine()

# Perform a query and print the response
response = query_engine.query("What does the author think about Star Trek? In One line")
print(response)

# Import modules
import chromadb
from llama_index import VectorStoreIndex, ServiceContext
from llama_index.llms import Ollama
from llama_index.vector_stores.chroma import ChromaVectorStore

# Create Chroma DB client and access the existing vector store
client = chromadb.PersistentClient(path="./chroma_db_data")
chroma_collection = client.get_collection(name="tweets")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)

# Initialize Ollama and ServiceContext
llm = Ollama(model="mistral")
service_context = ServiceContext.from_defaults(llm=llm, embed_model="local")

# Create VectorStoreIndex and query engine with a similarity threshold of 20
index = VectorStoreIndex.from_vector_store(vector_store=vector_store, service_context=service_context, similarity_top_k=20)
query_engine = index.as_query_engine()

# Perform a query and print the response
response = query_engine.query("What does the author think about Star Trek? In One line")
print(response)
RAG QA Database using Embeddings
Published: 2024-01-28 07:20:51

**RAG QA Database using Embeddings**



from datasets import load_dataset
from txtai import Embeddings

# Load dataset
ds = load_dataset("web_questions", split="train")

# Initialize and index embeddings
embeddings = Embeddings(path="sentence-transformers/nli-mpnet-base-v2", content=True)
embeddings.index([(uid, {"url": row["url"], "text": row["question"], 
                  "answer": ", ".join(row["answers"])}, None) 
                  for uid, row in enumerate(ds)])

# Save the embeddings
embeddings.save("questions.tar.gz")

# Search embeddings
def question(text):
    return embeddings.search(f"select text, answer, score from txtai where similar('{text}') limit 1")

print(question("Tell me an animal found offshore in Florida"))

User Interface

import gradio as gr
from txtai.app import Application

# Initialize the saved embeddings
app = Application("path: questions.tar.gz")

def search_question(query):
    results = app.search(f"select text, answer, score from txtai where similar('{query}') limit 1")[0]
    return results['text'], results['answer'], results['score']

interface = gr.Interface(
    fn=search_question,
    inputs=gr.components.Textbox(label="Enter your query"),
    outputs=[gr.components.Textbox(label="Question"),gr.components.Textbox(label="Answer"),gr.components.Number(label="Similarity")]
)

interface.launch()
🔥Build A Movie Search App Using Redux Toolkit And React Router 6🔥
Tags: react, javascript, codenewbie
Published: 2024-01-28 07:17:57

{% embed https://www.youtube.com/watch?v=TrGfZPoJNAg %}

In this 2 hour video, you will learn:  
  
✅ How to use TMDB API to fetch the list of movies by search keyword  
  
✅ How to mock API to avoid facing rate limiting issue  
  
✅ How to use Debouncing to avoid extra API calls  
  
✅ How to properly display the movies list on the screen  
  
✅ How to implement routing to display movie details page when clicked on the individual movie  
  
✅ How to debug application in various scenarios  
  
✅ How to do data manipulation to get the response in the correct format using array map, filter and reduce method  
  
and much more...

**This video is the final challenge video to practice React skills from my React course:** [**https://bit.ly/41SosrP**](https://bit.ly/41SosrP)

## **Thanks for Reading!**

If you want to master JavaScript, ES6+, React and Node.js with easy-to-understand content, do check out my [**YouTube channel**](https://www.youtube.com/@codingmastery_dev/) and don't forget to subscribe.

Want to stay up to date with regular content regarding JavaScript, React, Node.js? [**Follow me on LinkedIn**](https://www.linkedin.com/in/yogesh-chavan97/).
Flask App Serving Mistral + Llama Index + Ollama + ChromaDB -RAG
Published: 2024-01-28 07:17:27

**Flask App Serving Mistral + Llama Index + Ollama + ChromaDB**



# Import modules
import chromadb
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
from llama_index import VectorStoreIndex, ServiceContext
from llama_index.llms import Ollama
from llama_index.vector_stores.chroma import ChromaVectorStore

# Create Chroma DB client and access the existing vector store
client = chromadb.PersistentClient(path="./chroma_db_data")
chroma_collection = client.get_collection(name="tweets")
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)

# Initialize Ollama and ServiceContext
llm = Ollama(model="mistral")
service_context = ServiceContext.from_defaults(llm=llm, embed_model="local")

# Create VectorStoreIndex with a similarity threshold of 20
index = VectorStoreIndex.from_vector_store(vector_store=vector_store, service_context=service_context, similarity_top_k=20)

# Set up Flask server
app = Flask(__name__)
cors = CORS(app)
app.config['CORS_HEADERS'] = 'Content-Type'

@app.route('/')
def hello_world():
    return 'Hello, World!'

@app.route('/chat', methods=['GET', 'POST'])
@cross_origin()
def chat():
    query = request.args.get('query') if request.method == 'GET' else request.form.get('query')
    if query is not None:
        query_engine = index.as_query_engine()
        response = query_engine.query(query)
        return jsonify({"response": str(response)})
    else:
        return jsonify({"error": "query field is missing"}), 400
    
if __name__ == '__main__':
    app.run()
HTML "self-awareness" with sibling indexing in JavaScript
Tags: html, javascript
Published: 2024-01-28 07:05:00

---
title: HTML "self-awareness" with sibling indexing in JavaScript
published: true
description: Make your HTML more "self-aware" with a fun little trick to get its own index.
tags: html, javascript
published_at: 2024-01-28 07:05 +0000
canonical_url: https://blog.cassidoo.co/post/self-aware-html/
---


This is a super specific use case, but if you have an HTML element and you want it to be more "self-aware", you can do:

```js
let element = document.getElementById('whatever');

// To get an array of siblings
[...element.parentElement.children]

// To get current index of self
[...element.parentElement.children].indexOf(element)
```

What we're doing here is:
- Getting our HTML element
- Getting the parent of our HTML element
- Getting the children of the parent
- Spreading it into an array (because it's an `HTMLCollection` otherwise, you can also use [`Array.from`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from))
- Getting the index of our element amongst all of its siblings

I figured this out after working on a framework-less project, where I wanted an HTML `<button>` to be able to tell the function it called its own index.

Example HTML:
```html
<div>
	<button onclick="whoami(event)">Some button</button>
	<button onclick="whoami(event)">Some other button</button>
	<button onclick="whoami(event)">And other button</button>
</div>
```

And the corresponding example JavaScript:
```js
function whoami(event) {
	let element = event.currentTarget;
	let currentButtonIndex = [...element.parentElement.children].indexOf(element);
	// ...and so on
}
```

Anyway, I thought this was cool, hope it's useful for you!
Wikipedia Chat bot using Zypher 7B
Published: 2024-01-28 07:04:29



![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ek48efty8oosz1ewo5f2.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38fow8zopl0kchtmpi3y.png)
reading: Wrangling a Python monorepo
Tags: python, monorepo, learning
Published: 2024-01-28 06:55:03

Been a long time since I've wrangled a #Python deployment let alone a Python #monorepo. I found this series by Tweag.io invaluable:


**PYTHON MONOREPO: AN EXAMPLE**

[Part 1: Structure and Tooling](https://www.tweag.io/blog/2023-04-04-python-monorepo-1/)

[Part 2: A simple CI](https://www.tweag.io/blog/2023-07-13-python-monorepo-2/)
AutoGen Travel Agents
Published: 2024-01-28 05:52:41

**	
AutoGen Travel Agents **



Travel Agent Manager

You are a helpful assistant that can suggest a travel itinerary for a user. You are the primary coordinator who will receive suggestions or advice from other agents (local_assistant, language_assistant). You must ensure that the finally plan integrates the suggestions from other agents or team members. 

Expand this guide into a a full 7-day travel itinerary with detailed per-day plans, including weather forecasts, places to eat, packing suggestions, and a budget breakdown. You MUST suggest actual places to visit, actual hotels to stay and actual restaurants to go to. This itinerary should cover all aspects of the trip, from arrival to departure, integrating the city guide information with practical travel logistics. Your final answer MUST be a complete expanded travel plan, formatted as markdown, encompassing a daily schedule, anticipated weather conditions, recommended clothing and items to pack, and a detailed budget, ensuring THE BEST TRIP EVER, Be specific and give it a reason why you picked # up each place, what make them special!

YOUR FINAL RESPONSE MUST BE THE COMPLETE PLAN that ends with the word TERMINATE. 

City Selection Expert

Analyse and select the best city for the trip based on specific criteria such as weather patterns, seasonal events, and travel costs. Use the search_internet skill to know about weather patterns, seasonal events, and travel costs. This task involves comparing multiple cities, considering factors like current weather conditions, upcoming cultural or seasonal events, and overall travel expenses. Your final answer must be a detailed report on the chosen city, and everything you found out about it, including the actual flight costs, weather forecast and attractions.

Local Agent

As a local expert on this city you must compile an in-depth guide for someone traveling there and wanting to have THE BEST trip ever! Gather information about key attractions, local customs, special events, and daily activity recommendations using search_internet skill. Find the best spots to go to, the kind of place only a local would know. This guide should provide a thorough overview of what the city has to offer, including hidden gems, cultural hotspots, must-visit landmarks, weather forecasts, and high level costs. The final answer must be a comprehensive city guide, rich in cultural insights and practical tips, tailored to enhance the travel experience.

Search Tool

from duckduckgo_search import DDGS

def search_internet(query, max_results=1):
    """
    Search Skill
    Search Tool
    Search Internet Tool
    Search Internet Skill
    duckduckgo_search tool
    Performs a search on DuckDuckGo using the duckduckgo_search package.

    :param query: str, the search query.
    :param max_results: int, maximum number of results to return.
    :return: list, search results.
    """
    with DDGS() as ddgs:
        return [r for r in ddgs.text(query, max_results=max_results)]

# Example usage:
# results = search_internet('example query')
# print(results)
Default Lambda Parameters in C# 12
Tags: csharp
Published: 2024-01-28 06:44:54

Title: Exploring the Power of Default Lambda Parameters in C# 12

Introduction:
C# has always been an evolving language, constantly introducing new features and enhancements to improve developer productivity. In the upcoming C# 12 release, a new feature called "Default Lambda Parameters" is set to make writing functions even more flexible and reusable. In this post, we'll dive into the concept of default lambda parameters and explore how they enhance code readability and enable efficient usage of lambdas.

Understanding Default Lambda Parameters:
Default parameters have been around in C# for a while, allowing developers to specify default values for arguments in methods. With the introduction of C# 12, this feature is extended to lambda expressions, further extending the flexibility of this powerful language construct.

The beauty of default lambda parameters lies in their capability to assign default values directly to lambda expressions, making them self-contained and reusable. This means that you no longer have to rely on external variables or explicitly define argument values every time you use a lambda expression.

Code Example - Applying Default Lambda Parameters:
Let's take a look at a practical example to demonstrate how default lambda parameters can simplify our code.

```C#
using System;

public class Program
{
    public static void Main()
    {
        Func<int, int, int> sum = (x, y = 5) => x + y;
        
        Console.WriteLine(sum(10));      // Output: 15
        Console.WriteLine(sum(10, 7));   // Output: 17
    }
}
```

In the above code snippet, we define a lambda expression `sum` that takes two integer arguments `x` and `y`, with `y` having a default value of 5. If `y` is not explicitly passed during the function call, it will automatically default to 5.

Benefits of Default Lambda Parameters:
1. Enhanced Readability: By specifying default values directly within the lambda expression itself, the intention and behavior of the function become more apparent, reducing the need for external documentation or comments.

2. Reusability: Default lambda parameters enable you to define reusable lambda expressions that can be used in various scenarios without the need for additional customization. This promotes code modularity and improves overall code maintainability.

3. Simplified Function Flow: With default lambda parameters, you no longer have to worry about missing arguments or explicitly checking for null or empty values. The defaults take care of these edge cases, ensuring your code remains resilient and efficient.

Conclusion:
In the upcoming C# 12 release, the addition of default lambda parameters empowers developers to write more concise, readable, and reusable code. This feature further strengthens the versatility of lambda expressions, providing a powerful tool for expressing complex logic in a concise manner. By leveraging default lambda parameters, C# developers can boost productivity, reduce boilerplate code, and build highly modular applications.
Generate Text Phixtral inference
Published: 2024-01-28 06:25:57

**Phixtral inference**



Code to run mlabonne/phixtral-2x2_8

https://huggingface.co/mlabonne/phixtral-2x2_8 is recommended and mlabonne/phixtral-4x2_8 https://huggingface.co/mlabonne/phixtral-4x2_8 in 4-bit precision.

pip install -qqq --upgrade transformers einops accelerate bitsandbytes --progress-bar off

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer

model_name = "phixtral-2x2_8" # @param ["phixtral-2x2_8", "phixtral-4x2_8"]
instruction = "Write an epic poem about Ancient Rome." # @param {type:"string"}

prompt = f'''

system
You are Phixtral, a helpful AI assistant.


user
{instruction}


assistant
'''

torch.set_default_device("cuda")

# Load the model and tokenizer
model = AutoModelForCausalLM.from_pretrained(
    f"mlabonne/{model_name}",
    torch_dtype="auto",
    load_in_4bit=True,
    trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(
    f"mlabonne/{model_name}",
    trust_remote_code=True
)

# Tokenize the input string
inputs = tokenizer(
    prompt,
    return_tensors="pt",
    return_attention_mask=False
)

# Generate text using the model
outputs = model.generate(**inputs, max_length=200)

# Decode and print the output
text = tokenizer.batch_decode(outputs)[0]
print(text[len(prompt):])
CrewAI Example Code
Published: 2024-01-28 06:23:51

**CrewAI Example Code**



openai

export OPENAI_API_KEY=xxxxxxxxxxxxxxxxx

import os
from crewai import Agent, Task, Crew, Process
from langchain.tools import DuckDuckGoSearchRun
search_tool = DuckDuckGoSearchRun()

# Define your agents with roles and goals
researcher = Agent(
  role='Senior Research Analyst',
  goal='Uncover cutting-edge developments in AI and data science',
  backstory="""You are an expert at a technology research group, 
  skilled in identifying trends and analyzing complex data.""",
  verbose=True,
  allow_delegation=False,
  tools=[search_tool]
)
writer = Agent(
  role='Tech Content Strategist',
  goal='Craft compelling content on tech advancements',
  backstory="""You are a content strategist known for 
  making complex tech topics interesting and easy to understand.""",
  verbose=True,
  allow_delegation=True,
)

# Create tasks for your agents
task1 = Task(
  description="""Analyze 2024's AI advancements. 
  Find major trends, new technologies, and their effects. 
  Provide a detailed report.""",
  agent=researcher
)

task2 = Task(
  description="""Create a blog post about major AI advancements using your insights. 
  Make it interesting, clear, and suited for tech enthusiasts. 
  It should be at least 4 paragraphs long.""",
  agent=writer
)

# Instantiate your crew with a sequential process
crew = Crew(
  agents=[researcher, writer],
  tasks=[task1, task2],
  verbose=2
)

# Get your crew to work!
result = crew.kickoff()

print("######################")
print(result)

Ollama

import os
from crewai import Agent, Task, Crew, Process
from langchain.tools import DuckDuckGoSearchRun
search_tool = DuckDuckGoSearchRun()
from langchain.llms import Ollama
ollama_llm = Ollama(model="mistral")
ollama_llm_orca2 = Ollama(model="orca2")

# Define your agents with roles and goals
researcher = Agent(
  role='Senior Research Analyst',
  goal='Uncover cutting-edge developments in AI and data science',
  backstory="""You are an expert at a technology research group, 
  skilled in identifying trends and analyzing complex data.""",
  verbose=True,
  allow_delegation=False,
  tools=[search_tool],
  llm=ollama_llm
)
writer = Agent(
    role='Tech Content Strategist',
    goal='Craft compelling content on tech advancements',
    backstory="""You are a content strategist known for 
    making complex tech topics interesting and easy to understand.""",
    verbose=True,
    allow_delegation=True,
    llm=ollama_llm_orca2
)

# Create tasks for your agents
task1 = Task(
  description="""Analyze 2024's AI advancements. 
  Find major trends, new technologies, and their effects. 
  Provide a detailed report.""",
  agent=researcher
)

task2 = Task(
    description="""Create a blog post about major AI advancements using your insights. 
    Make it interesting, clear, and suited for tech enthusiasts. 
    It should be at least 4 paragraphs long.""",
    agent=writer
)

# Instantiate your crew with a sequential process
crew = Crew(
    agents=[researcher, writer],
    tasks=[task1, task2],
    verbose=2
)

# Get your crew to work!
result = crew.kickoff()

print("######################")
print(result)
Crew AI Stock Analysis
Published: 2024-01-28 06:21:31

Crew AI Stock Analysis

    

git clone https://github.com/joaomdmoura/crewAI-examples
cd crewAI-examples
cd stock_analysis
conda create -n crewai python=3.11 -y
conda activate crewai
pip install poetry platformdirs
poetry install --no-root
poetry run python main.py

Code fix as in the video

/Users/USER/miniconda3/envs/creawai/lib/python3.11/site-packages/langchain_community/tools/yahoo_finance_news.py

@staticmethod
    def _format_results(docs: Iterable[Document], query: str) -> str:
        doc_strings = [
            "\n".join([doc.metadata["title"], doc.metadata.get("description", "")])
            for doc in docs
            if query in doc.metadata.get("description", "") or query in doc.metadata["title"]
        ]
        return "\n\n".join(doc_strings)

Categories
Crew AI Instagram Post
Published: 2024-01-28 06:20:27

**Crew AI Instagram Post**



git clone https://github.com/joaomdmoura/crewAI-examples
cd crewAI-examples
cd instagram_post
conda create -n crewai python=3.11 -y
conda activate crewai
export OPENAI_API_KEY=st-xxxxxxxxxxxxxxxxxxxxx
pip install poetry platformdirs
poetry install --no-root
poetry run python main.py
Mixtral Pricing Compare
Published: 2024-01-28 06:19:15

**Mixtral Pricing Compare**



prices and providers launch Mixtral with (prices below are quoted as ($ per million tokens input / $ per million tokens output):

    ($0.6/$1.8) from Mistral
    ($0.4/$1.6) from Fireworks.AI
    ($0.6/$0.6) from Together AI
    ($0.14/0.56) from Perplexity 
    ($0.5/$0.5) from Anyscale 
    ($0.2/$0.5) from OctoAI (formerly OctoML)
    ($0.3/$0.3) from Abacus AI – down from original ($2/$2)!
    ($0.27/$0.27) from Deepinfra
    “free” on OpenRouter but rate limited