hadxu commited on
Commit
bebbb83
1 Parent(s): cb594b3

add openrouter api

Browse files
Files changed (1) hide show
  1. app.py +11 -51
app.py CHANGED
@@ -1,23 +1,14 @@
1
- import urllib.request
2
- import fitz
3
- import re
4
- import numpy as np
5
- import tensorflow_hub as hub
6
  from openai import OpenAI
7
  import gradio as gr
8
  import os
9
  import shutil
10
  from pathlib import Path
11
  from tempfile import NamedTemporaryFile
12
- from sklearn.neighbors import NearestNeighbors
13
- import anthropic
14
 
15
- # client = OpenAI(
16
- # base_url='https://api-inference.huggingface.co/models/mistralai/Mixtral-8x7B-Instruct-v0.1/v1/',
17
- # api_key=os.getenv('openai_key')
18
- # )
19
-
20
- client = anthropic.Anthropic()
21
 
22
  from util import pdf_to_text, text_to_chunks, SemanticSearch
23
 
@@ -30,45 +21,14 @@ def load_recommender(path, start_page=1):
30
  return 'Corpus Loaded.'
31
 
32
 
33
- # def openai_generate_text(prompt, model = "gpt-3.5-turbo-16k-0613"):
34
- # model="mistralai/Mixtral-8x7B-Instruct-v0.1"
35
- # max_tokens=1024
36
- # message = clinet.chat.completions.create(
37
- # model=model,
38
- # messages=[
39
- # {"role": "user", "content": prompt}
40
- # ],
41
- # max_tokens=max_tokens,
42
- # ).choices[0].message.content
43
- # return message
44
-
45
- def claude_generate_text(prompt, model = "claude-3-haiku-20240307"):
46
- message = client.messages.create(
47
- model=model,
48
- max_tokens=1000,
49
- temperature=0.0,
50
- # system="Respond only in mandarin",
51
  messages=[
52
  {"role": "user", "content": prompt}
53
- ]
54
- )
55
- return message.content[0].text
56
-
57
- def generate_answer(question):
58
- topn_chunks = recommender(question)
59
- prompt = 'search results:\n\n'
60
- for c in topn_chunks:
61
- prompt += c + '\n\n'
62
-
63
- prompt += "Instructions: Compose a comprehensive reply to the query using the search results given. "\
64
- "Cite each reference using [ Page Number] notation. "\
65
- "Only answer what is asked. The answer should be short and concise. "\
66
- "If asked in Chinese, respond in Chinese; if asked in English, respond"\
67
- "in English \n\nQuery: "
68
-
69
- prompt += f"{question}\nAnswer:"
70
- answer = claude_generate_text(prompt)
71
- return answer
72
 
73
  def question_answer(chat_history, file, question):
74
  suffix = Path(file.name).suffix
@@ -77,7 +37,7 @@ def question_answer(chat_history, file, question):
77
  tmp_path = Path(tmp.name)
78
 
79
  load_recommender(str(tmp_path))
80
- answer = generate_answer(question)
81
  chat_history.append([question, answer])
82
  return chat_history
83
 
 
 
 
 
 
 
1
  from openai import OpenAI
2
  import gradio as gr
3
  import os
4
  import shutil
5
  from pathlib import Path
6
  from tempfile import NamedTemporaryFile
 
 
7
 
8
+ client = OpenAI(
9
+ base_url="https://openrouter.ai/api/v1",
10
+ api_key=os.getenv('OPENROUTER_API_KEY')
11
+ )
 
 
12
 
13
  from util import pdf_to_text, text_to_chunks, SemanticSearch
14
 
 
21
  return 'Corpus Loaded.'
22
 
23
 
24
+ def generate_text(prompt):
25
+ message = client.chat.completions.create(
26
+ model="google/gemini-pro",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
27
  messages=[
28
  {"role": "user", "content": prompt}
29
+ ],
30
+ ).choices[0].message.content
31
+ return message
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
32
 
33
  def question_answer(chat_history, file, question):
34
  suffix = Path(file.name).suffix
 
37
  tmp_path = Path(tmp.name)
38
 
39
  load_recommender(str(tmp_path))
40
+ answer = generate_text(question)
41
  chat_history.append([question, answer])
42
  return chat_history
43