-
Notifications
You must be signed in to change notification settings - Fork 0
/
raw_rag.py
142 lines (115 loc) · 4 KB
/
raw_rag.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import ollama
import litellm
import uuid
import os
import pandas as pd
from sqlalchemy import create_engine, text
from llama_index.llms.litellm import LiteLLM
from dotenv import load_dotenv
jobs = pd.read_csv("1713798254.834989-combined-data_scientist.csv")
job_data = jobs["description"].tolist()
len(job_data)
load_dotenv()
POSTGRES_CONN_STRING = os.getenv("POSTGRES_CONNECTION")
# Set up litellm callbacks and verbosity
litellm.success_callback = ["langfuse"]
litellm.failure_callback = ["langfuse"]
litellm.set_verbose = False
# Function to generate embeddings using the ollama library
def embed_arctic(prompt):
return ollama.embeddings(model="snowflake-arctic-embed:latest", prompt=prompt)
# Function to insert job descriptions and their embeddings into the database
def insert_embeddings(conn, dd):
for i, d in enumerate(dd):
if i > 200:
break
print(i)
out = embed_arctic(d)
stmt = text(
"""INSERT INTO embed_descriptions (description, embedding) VALUES(:description, :embedding)"""
)
package = {"description": d, "embedding": out["embedding"]}
with conn.connect() as c:
c.execute(stmt, package)
c.commit()
# Function to perform a similarity search on the embeddings
def similarity_search(conn):
query = "**Federal Project - Applicant must be a United States Citizen or Permanent Residents, with the ability to obtain a Public Trust**"
embedded_q = embed_arctic(query)
package_1 = {"q_embed": str(embedded_q["embedding"])}
find_stmt = text(
"""SELECT description from embed_descriptions ORDER BY embedding <-> :q_embed LIMIT 2"""
)
with conn.connect() as c:
result = c.execute(find_stmt, package_1)
for r in result:
print(r)
# Function to perform a cosine similarity search on the embeddings
def cosine_search(conn):
query = """ What remote jobs have salaries greater than 150k """
embedded_q = embed_arctic(query)
package_2 = {"q_embed": str(embedded_q["embedding"])}
find_stmt = text(
"""Select 1 - (embedding <=> :q_embed) as cosine_similarity, description from embed_descriptions order by cosine_similarity desc Limit 50"""
)
with conn.connect() as c:
result = c.execute(find_stmt, package_2)
return [result, query]
# Main Area
# Read job descriptions from a CSV file
description_df = pd.read_csv("ai_engineer_2024-04-12_01-01-59-873.csv")
dd = description_df["description"].to_list()
# Chunk up a document
to_chunk = dd[0]
size = 1000
overlap = 100
chunks = []
current_chunk = ""
for idx, c in enumerate(to_chunk):
pass
# Connect to a PostgreSQL database
conn = create_engine(POSTGRES_CONN_STRING)
# Perform cosine similarity search
result = cosine_search(conn)
# Store the top results
top_results = []
for r in result[0]:
print(r)
top_results.append(r[1])
# Generate a unique identifier (UUID) for tracing purposes
current_uuid = uuid.uuid4()
# Prepare additional keyword arguments for LiteLLM models
llm_kwargs = {
"metadata": {
"generation_name": "rag_generation",
"trace_name": "RAG_task_haiku_instruct",
"version": "0.0.1",
"trace_id": str(current_uuid),
"input": {
"documents": str(top_results),
"prompt": result[1],
},
},
}
# Initialize LiteLLM models with different model configurations
llama_3_llm = LiteLLM(
model="together_ai/microsoft/WizardLM-2-8x22B",
additional_kwargs=llm_kwargs,
)
claude_llm = LiteLLM(
model="claude-3-haiku-20240307",
max_tokens=4000,
additional_kwargs=llm_kwargs,
)
qwen_llm = LiteLLM(
model="ollama/command-r:latest",
additional_kwargs=llm_kwargs,
)
# Prepare the context using the top results
context = f"Context: {str(top_results)}"
# Use the claude_llm model to generate a response based on the context and the question
out = qwen_llm.complete(
f"{context}\nOnly use the given Context to answer the question:\n {result[1]} "
)
# Print the generated response
print(out)