wakeupmh commited on
Commit
34688e7
·
1 Parent(s): b0e0ada

fix: using kimi

Browse files
Files changed (1) hide show
  1. services/model_handler.py +34 -23
services/model_handler.py CHANGED
@@ -1,13 +1,20 @@
1
  import logging
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import streamlit as st
4
- from agno.agent import Agent
5
  from agno.models.ollama import Ollama
6
  from agno.tools.arxiv import ArxivTools
7
  from agno.tools.pubmed import PubmedTools
 
 
8
 
9
- MODEL_PATH = "meta-llama/Llama-3.2-1B"
10
-
 
 
 
 
 
11
  class ModelHandler:
12
  def __init__(self):
13
  """Initialize the model handler"""
@@ -21,11 +28,14 @@ class ModelHandler:
21
 
22
  def _initialize_model(self):
23
  """Initialize model and tokenizer"""
24
- self.model, self.tokenizer = self._load_model()
25
  self.translator = Agent(
26
  name="Translator",
27
  role="You will translate the query to English",
28
- model=Ollama(id="llama3.2:1b"),
 
 
 
 
29
  goal="Translate to English",
30
  instructions=[
31
  "Translate the query to English"
@@ -35,12 +45,17 @@ class ModelHandler:
35
  self.researcher = Agent(
36
  name="Researcher",
37
  role="You are a research scholar who specializes in autism research.",
38
- model=Ollama(id="llama3.2:1b"),
 
 
 
 
39
  tools=[ArxivTools(), PubmedTools()],
40
  instructions=[
41
- "You need to understand the context of the question to provide the best answer based on your tools."
42
- "Be precise and provide just enough information to be useful",
43
- "You must cite the sources used in your answer."
 
44
  "You must create an accessible summary.",
45
  "The content must be for people without autism knowledge.",
46
  "Focus in the main findings of the paper taking in consideration the question.",
@@ -51,7 +66,11 @@ class ModelHandler:
51
  self.summarizer = Agent(
52
  name="Summarizer",
53
  role="You are a specialist in summarizing research papers for people without autism knowledge.",
54
- model=Ollama(id="llama3.2:1b"),
 
 
 
 
55
  instructions=[
56
  "You must provide just enough information to be useful",
57
  "You must cite the sources used in your answer.",
@@ -74,7 +93,11 @@ class ModelHandler:
74
  self.presenter = Agent(
75
  name="Presenter",
76
  role="You are a professional researcher who presents the results of the research.",
77
- model=Ollama(id="llama3.2:1b"),
 
 
 
 
78
  instructions=[
79
  "You are multilingual",
80
  "You must present the results in a clear and concise manner.",
@@ -90,18 +113,6 @@ class ModelHandler:
90
  )
91
 
92
 
93
- @staticmethod
94
- @st.cache_resource
95
- @st.cache_data
96
- def _load_model():
97
- try:
98
- tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
99
- model = AutoModelForCausalLM.from_pretrained(MODEL_PATH)
100
- return model, tokenizer
101
- except Exception as e:
102
- logging.error(f"Error loading model: {str(e)}")
103
- return None, None
104
-
105
  def generate_answer(self, query: str) -> str:
106
  try:
107
  translator = self.translator.run(query, stream=False)
 
1
  import logging
2
  from transformers import AutoTokenizer, AutoModelForCausalLM
3
  import streamlit as st
4
+ from agno.models.openai import OpenAILike
5
  from agno.models.ollama import Ollama
6
  from agno.tools.arxiv import ArxivTools
7
  from agno.tools.pubmed import PubmedTools
8
+ import os
9
+ import random
10
 
11
+ API_KEYS = [
12
+ os.getenv("SK1"),
13
+ os.getenv("SK2"),
14
+ os.getenv("SK3"),
15
+ os.getenv("SK4"),
16
+ os.getenv("SK5")
17
+ ]
18
  class ModelHandler:
19
  def __init__(self):
20
  """Initialize the model handler"""
 
28
 
29
  def _initialize_model(self):
30
  """Initialize model and tokenizer"""
 
31
  self.translator = Agent(
32
  name="Translator",
33
  role="You will translate the query to English",
34
+ model=OpenAILike(
35
+ api_key=random.choice(API_KEYS),
36
+ base_url="https://api.moonshot.cn/v1",
37
+ id="moonshot-v1-8k"
38
+ ),
39
  goal="Translate to English",
40
  instructions=[
41
  "Translate the query to English"
 
45
  self.researcher = Agent(
46
  name="Researcher",
47
  role="You are a research scholar who specializes in autism research.",
48
+ model=OpenAILike(
49
+ api_key=random.choice(API_KEYS),
50
+ base_url="https://api.moonshot.cn/v1",
51
+ id="moonshot-v1-8k"
52
+ ),
53
  tools=[ArxivTools(), PubmedTools()],
54
  instructions=[
55
+ "You have ArxivTools and PubmedTools at your disposal. Use them to find relevant papers for the question.",
56
+ "You need to understand the context of the question to provide the best answer based on your tools.",
57
+ "Be precise and provide just enough information to be useful.",
58
+ "You must cite the sources used in your answer.",
59
  "You must create an accessible summary.",
60
  "The content must be for people without autism knowledge.",
61
  "Focus in the main findings of the paper taking in consideration the question.",
 
66
  self.summarizer = Agent(
67
  name="Summarizer",
68
  role="You are a specialist in summarizing research papers for people without autism knowledge.",
69
+ model=OpenAILike(
70
+ api_key=random.choice(API_KEYS),
71
+ base_url="https://api.moonshot.cn/v1",
72
+ id="moonshot-v1-8k"
73
+ ),
74
  instructions=[
75
  "You must provide just enough information to be useful",
76
  "You must cite the sources used in your answer.",
 
93
  self.presenter = Agent(
94
  name="Presenter",
95
  role="You are a professional researcher who presents the results of the research.",
96
+ model=OpenAILike(
97
+ api_key=random.choice(API_KEYS),
98
+ base_url="https://api.moonshot.cn/v1",
99
+ id="moonshot-v1-8k"
100
+ ),
101
  instructions=[
102
  "You are multilingual",
103
  "You must present the results in a clear and concise manner.",
 
113
  )
114
 
115
 
 
 
 
 
 
 
 
 
 
 
 
 
116
  def generate_answer(self, query: str) -> str:
117
  try:
118
  translator = self.translator.run(query, stream=False)