Spaces:
Runtime error
Runtime error
Pinecone integration
Browse files
app.py
CHANGED
|
@@ -1,8 +1,47 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
return "Hello " + message + "!!"
|
| 5 |
-
return message
|
| 6 |
-
|
| 7 |
-
iface = gr.ChatInterface(greet, chatbot=gr.Chatbot(height=300), textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7), title="Amazon Bedrock and Titan LLM", theme="soft", examples=[ "Who won the best music award?", "Which award did Avatar win?", "Who won the Best Actor award in a supporting role?", "Who is the lyricist for the song Natu Natu from RRR?", "How many awards did the film RRR win?", "Which was the Best International Feature Film?",], cache_examples=False, retry_btn=None, undo_btn="Delete Previous", clear_btn="Clear",)
|
| 8 |
iface.launch(share=True)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import openai
|
| 3 |
+
import json
|
| 4 |
+
from pinecone import Pinecone
|
| 5 |
+
pc = Pinecone(api_key='72f7d24d-c080-4ce6-a060-a489713284cb')
|
| 6 |
+
index_name = 'serverless-index'
|
| 7 |
+
# connect to index
|
| 8 |
+
index = pc.Index(index_name)
|
| 9 |
+
# get api key from platform.openai.com
|
| 10 |
+
openai.api_key = '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'
|
| 11 |
+
embed_model = "text-embedding-ada-002"
|
| 12 |
+
from openai import OpenAI
|
| 13 |
+
def generate_context(text):
|
| 14 |
+
body=json.dumps({"inputText": text})
|
| 15 |
+
client = OpenAI(api_key=openai.api_key)
|
| 16 |
+
res = client.embeddings.create(
|
| 17 |
+
input=[text],
|
| 18 |
+
model=embed_model
|
| 19 |
+
).data[0].embedding
|
| 20 |
+
result = index.query(vector=res, top_k=7, include_metadata=True)
|
| 21 |
+
contexts = []
|
| 22 |
+
contexts = contexts + [x['metadata']['text'] for x in result['matches']]
|
| 23 |
+
return contexts
|
| 24 |
+
|
| 25 |
+
def invoke_openai(prompt):
|
| 26 |
+
sys_prompt = "You are a helpful assistant that always answers questions."
|
| 27 |
+
# query text-davinci-003
|
| 28 |
+
res = client.chat.completions.create(
|
| 29 |
+
model='gpt-3.5-turbo-0613',
|
| 30 |
+
messages=[
|
| 31 |
+
{"role": "system", "content": sys_prompt},
|
| 32 |
+
{"role": "user", "content": prompt}
|
| 33 |
+
],
|
| 34 |
+
temperature=0
|
| 35 |
+
)
|
| 36 |
+
return res.choices[0].message.content
|
| 37 |
+
|
| 38 |
+
def build_prompt(message,history):
|
| 39 |
+
context=generate_context(message)
|
| 40 |
+
messages=[]
|
| 41 |
+
prompt=f'Context - {context}\nBased on the above context, answer this question - {message}'
|
| 42 |
+
print(prompt)
|
| 43 |
+
return invoke_openai(prompt)
|
| 44 |
+
|
| 45 |
|
| 46 |
+
iface = gr.ChatInterface(build_prompt, chatbot=gr.Chatbot(height=300), textbox=gr.Textbox(placeholder="Ask me a question", container=False, scale=7), title="Amazon Bedrock and Titan LLM", theme="soft", examples=[ "Who won the best music award?", "Which award did Avatar win?", "Who won the Best Actor award in a supporting role?", "Who is the lyricist for the song Natu Natu from RRR?", "How many awards did the film RRR win?", "Which was the Best International Feature Film?",], cache_examples=False, retry_btn=None, undo_btn="Delete Previous", clear_btn="Clear",)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
iface.launch(share=True)
|