-
Notifications
You must be signed in to change notification settings - Fork 1
/
multimodal_RAG.py
77 lines (48 loc) · 1.62 KB
/
multimodal_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
import requests
import json
from langchain_ollama.llms import OllamaLLM
import embed_anything
from pinecone import Pinecone, ServerlessSpec
import numpy as np
import os
from pinecone import PineconeApiException
import uuid
from embed_anything import EmbedData
from fastapi import FastAPI
from pydantic import BaseModel
from embed_anything.vectordb import PineconeAdapter
import os
from langchain_ollama import ChatOllama
from langchain_core.messages import AIMessage
from langchain.chains import LLMChain
from fastapi import FastAPI, HTTPException
from embed_anything.vectordb import PineconeAdapter
ollama = OllamaLLM(base_url='http://localhost:11434', model='llama2')
docs = embed_anything.embed_webpage(url="PUT-LINK", embeder="Jina")
pc = Pinecone(api_key)
# Initialize the PineconeEmbedder class
api_key = os.environ.get("PINECONE_API_KEY")
index_name = "anything"
pinecone_adapter = PineconeAdapter(api_key)
try:
pinecone_adapter.delete_index("anything")
except:
pass
# Initialize the PineconeEmbedder class
pinecone_adapter.create_index(dimension=768, metric="cosine")
app = FastAPI()
class Query(BaseModel):
prompt: str
@app.post("/ollama")
async def run_ollama(query: Query):
embed_query = embed_anything.embed_query([query.prompt], embeder="Jina")
result = index_name.query(
vector=embed_query[0].embedding,
top_k=2,
include_metadata=True,
)
print(result)
metadata_texts = [item['metadata']['text'] for item in result['matches']]
joined_text = ''.join(query.prompt) + ' '.join(metadata_texts[0])
response = ollama(joined_text)
return response