Mohammed Albrayh commited on
Commit
a8dc960
1 Parent(s): 8f1ff57
Files changed (5) hide show
  1. .env +7 -0
  2. .gitignore +1 -0
  3. app.py +81 -48
  4. redme.md +0 -0
  5. requirements.txt +10 -1
.env ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ RUNPOD_KEY = 80SUW0PDAG3SDFSS2JA0E5QFCIMECU7SCXUTNH3A
2
+ RUNPOD_URL = https://api.runpod.ai/v2/vllm-s4x8uw9y3qv296/openai/v1
3
+ # QDRANT_URL = https://2fee3feb-59b4-43ca-adef-ffaaacd4e0c6.europe-west3-0.gcp.cloud.qdrant.io
4
+ QDRANT_URL = https://7fcf6c04-2f9f-40f3-b26b-c19885487d64.europe-west3-0.gcp.cloud.qdrant.io
5
+ # QDRANT_KEY = X2zkjP-ruS0uvdDEWbj2ZxeMVtmDbG1WXH6lQdGUnOYjT_-ODzqWZQ
6
+ QDRANT_KEY = c-p0ej858dskulydMoLpokCLK2Q2phWXTUHjrKBYYRz3y-g7YSKZ4A
7
+ OPENAI_API_KEY = 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
.gitignore ADDED
@@ -0,0 +1 @@
 
 
1
+ .env
app.py CHANGED
@@ -1,62 +1,95 @@
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
 
 
 
 
 
 
3
 
4
- """
5
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
6
- """
7
- client = InferenceClient("HuggingFaceH4/zephyr-7b-beta")
 
8
 
 
 
9
 
10
- def respond(
11
- message,
12
- history: list[tuple[str, str]],
13
- system_message,
14
- max_tokens,
15
- temperature,
16
- top_p,
17
- ):
18
- messages = [{"role": "system", "content": system_message}]
19
 
20
- for val in history:
21
- if val[0]:
22
- messages.append({"role": "user", "content": val[0]})
23
- if val[1]:
24
- messages.append({"role": "assistant", "content": val[1]})
25
 
26
- messages.append({"role": "user", "content": message})
 
27
 
28
- response = ""
 
 
 
29
 
30
- for message in client.chat_completion(
31
- messages,
32
- max_tokens=max_tokens,
33
- stream=True,
34
- temperature=temperature,
35
- top_p=top_p,
36
- ):
37
- token = message.choices[0].delta.content
38
 
39
- response += token
40
- yield response
41
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  """
43
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
44
- """
45
- demo = gr.ChatInterface(
46
- respond,
47
- additional_inputs=[
48
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
49
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
50
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
51
- gr.Slider(
52
- minimum=0.1,
53
- maximum=1.0,
54
- value=0.95,
55
- step=0.05,
56
- label="Top-p (nucleus sampling)",
57
- ),
58
- ],
59
- )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
 
61
 
62
  if __name__ == "__main__":
 
1
  import gradio as gr
2
+ from openai import OpenAI
3
+ import os
4
+ from dotenv import load_dotenv
5
+ from langchain.prompts import PromptTemplate
6
+ from qdrant_client import QdrantClient
7
+ from qdrant_client.http.models import Distance, VectorParams
8
+ from langchain_qdrant import QdrantVectorStore
9
+ from langchain_openai import OpenAIEmbeddings
10
 
11
+ load_dotenv()
12
+
13
+ RUNPOD_KEY = os.getenv("RUNPOD_KEY")
14
+ RUNPOD_URL = os.getenv("RUNPOD_URL")
15
+ model = OpenAI(api_key=RUNPOD_KEY, base_url= RUNPOD_URL)
16
 
17
+ QDRANT_URL = os.getenv("QDRANT_URL")
18
+ QDRANT_KEY = os.getenv("QDRANT_KEY")
19
 
20
+ print(QDRANT_URL)
21
+ print(QDRANT_KEY)
 
 
 
 
 
 
 
22
 
23
+ OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
24
+ print(OPENAI_API_KEY)
 
 
 
25
 
26
+ client = QdrantClient(QDRANT_URL, api_key=QDRANT_KEY)
27
+ collection_name = "search_engine"
28
 
29
+ embeddings = OpenAIEmbeddings(
30
+ model="text-embedding-3-small",
31
+ openai_api_key=OPENAI_API_KEY
32
+ )
33
 
34
+ qdrant = QdrantVectorStore(
35
+ client=client,
36
+ collection_name=collection_name,
37
+ embedding=embeddings
38
+ )
 
 
 
39
 
 
 
40
 
41
+
42
+
43
+ promtp_template = """
44
+ Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request.
45
+
46
+ ### Instruction:
47
+ {instruction}
48
+
49
+ ### Input:
50
+ {input}
51
+
52
+ ### Response:
53
  """
54
+
55
+ prompt = PromptTemplate(
56
+ input_variables=["instruction", "input"],
57
+ template=promtp_template,
58
+ )
59
+
60
+ def prompt_template(query):
61
+
62
+ results = qdrant.similarity_search( query=query, k=3 )
63
+
64
+ _ctx = ''
65
+
66
+ for i, result in enumerate(results):
67
+ _ctx += f'Content {i}: {result.page_content}\n-----\n'
68
+
69
+ _prompt = prompt.format(instruction=query, input=_ctx)
70
+
71
+ return _prompt
72
+
73
+ def generate_response(prompt):
74
+
75
+ response = model.chat.completions.create(
76
+ model="cenrak/llama3.1_fineTuned_model",
77
+ messages=[
78
+ {"role": "system", "content": "You are a helpful assistant."},
79
+ {"role": "user", "content": prompt},
80
+ ]
81
+ )
82
+
83
+ return response.choices[0].message
84
+
85
+ def main(query, history):
86
+ prompt = prompt_template(query)
87
+ resault = generate_response(prompt)
88
+
89
+
90
+ return resault.content
91
+
92
+ demo = gr.ChatInterface(fn=main, title = "News GPT")
93
 
94
 
95
  if __name__ == "__main__":
redme.md ADDED
File without changes
requirements.txt CHANGED
@@ -1 +1,10 @@
1
- huggingface_hub==0.22.2
 
 
 
 
 
 
 
 
 
 
1
+ openai
2
+ python-dotenv
3
+ spacy
4
+ langchain
5
+ langchain_community
6
+ langchain-openai
7
+ langchain-qdrant
8
+ qdrant-client
9
+ tqdm
10
+ spacy