Shreyas094 commited on
Commit
d3d8817
·
verified ·
1 Parent(s): 1a36c36

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +32 -1
app.py CHANGED
@@ -18,6 +18,8 @@ import inspect
18
  # Environment variables and configurations
19
  huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
20
  llama_cloud_api_key = os.environ.get("LLAMA_CLOUD_API_KEY")
 
 
21
 
22
  MODELS = [
23
  "Qwen/Qwen2-72B-Instruct",
@@ -26,7 +28,8 @@ MODELS = [
26
  "Qwen/Qwen2-7B-Instruct",
27
  "mistralai/Mistral-Nemo-Instruct-2407",
28
  "mistralai/Mistral-7B-Instruct-v0.3",
29
- "mistralai/Mixtral-8x7B-Instruct-v0.1"
 
30
  ]
31
 
32
  # Initialize LlamaParse
@@ -82,6 +85,9 @@ def update_vectors(files, parser):
82
  return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
83
 
84
  def generate_chunked_response(prompt, model, max_tokens=1000, max_chunks=5, temperature=0.7):
 
 
 
85
  client = InferenceClient(
86
  model,
87
  token=huggingface_token,
@@ -111,6 +117,31 @@ def generate_chunked_response(prompt, model, max_tokens=1000, max_chunks=5, temp
111
 
112
  return clean_response
113
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
114
  def duckduckgo_search(query):
115
  with DDGS() as ddgs:
116
  results = ddgs.text(query, max_results=5)
 
18
  # Environment variables and configurations
19
  huggingface_token = os.environ.get("HUGGINGFACE_TOKEN")
20
  llama_cloud_api_key = os.environ.get("LLAMA_CLOUD_API_KEY")
21
+ CLOUDFLARE_ACCOUNT_ID = os.environ.get("CLOUDFLARE_ACCOUNT_ID")
22
+ CLOUDFLARE_AUTH_TOKEN = os.environ.get("CLOUDFLARE_AUTH_TOKEN")
23
 
24
  MODELS = [
25
  "Qwen/Qwen2-72B-Instruct",
 
28
  "Qwen/Qwen2-7B-Instruct",
29
  "mistralai/Mistral-Nemo-Instruct-2407",
30
  "mistralai/Mistral-7B-Instruct-v0.3",
31
+ "mistralai/Mixtral-8x7B-Instruct-v0.1",
32
+ "cloudflare/llama-3.1-8b-instruct" # Added Cloudflare Llama 3.1 model
33
  ]
34
 
35
  # Initialize LlamaParse
 
85
  return f"Vector store updated successfully. Processed {total_chunks} chunks from {len(files)} files using {parser}."
86
 
87
  def generate_chunked_response(prompt, model, max_tokens=1000, max_chunks=5, temperature=0.7):
88
+ if model == "cloudflare/llama-3.1-8b-instruct":
89
+ return generate_cloudflare_response(prompt, max_tokens, temperature)
90
+
91
  client = InferenceClient(
92
  model,
93
  token=huggingface_token,
 
117
 
118
  return clean_response
119
 
120
+ def generate_cloudflare_response(prompt, max_tokens, temperature):
121
+ try:
122
+ response = requests.post(
123
+ f"https://api.cloudflare.com/client/v4/accounts/{CLOUDFLARE_ACCOUNT_ID}/ai/run/@cf/meta/llama-3.1-8b-instruct",
124
+ headers={"Authorization": f"Bearer {CLOUDFLARE_AUTH_TOKEN}"},
125
+ json={
126
+ "messages": [
127
+ {"role": "system", "content": "You are a friendly assistant"},
128
+ {"role": "user", "content": prompt}
129
+ ],
130
+ "max_tokens": max_tokens,
131
+ "temperature": temperature
132
+ }
133
+ )
134
+ result = response.json()
135
+ if 'result' in result and 'response' in result['result']:
136
+ return result['result']['response']
137
+ else:
138
+ print(f"Unexpected response format: {result}")
139
+ return "Error: Unexpected response format from Cloudflare API"
140
+ except Exception as e:
141
+ print(f"Error in generating Cloudflare response: {str(e)}")
142
+ return f"Error: {str(e)}"
143
+
144
+
145
  def duckduckgo_search(query):
146
  with DDGS() as ddgs:
147
  results = ddgs.text(query, max_results=5)