File size: 4,035 Bytes
25dc001
 
e31a853
a41e4c6
25dc001
9822412
 
988a46d
4bb21f8
9822412
971032f
 
9822412
7b99083
9822412
 
 
 
971032f
a41e4c6
 
971032f
1ed8fba
971032f
4bb21f8
 
7b99083
 
 
971032f
b9c182b
a41e4c6
1667843
25dc001
 
a41e4c6
07d335e
25dc001
 
b9c182b
25dc001
 
 
b9c182b
1667843
 
4ae53f7
1667843
4ae53f7
912a22e
 
9822412
971032f
25dc001
b9c182b
25dc001
 
 
 
 
 
 
 
b9c182b
 
25dc001
 
 
 
 
83083b0
9822412
988a46d
83083b0
988a46d
 
83083b0
1667843
4ae53f7
 
912a22e
 
07d335e
 
b9c182b
07d335e
 
 
 
 
b9c182b
07d335e
 
 
 
e31a853
 
 
07d335e
 
 
 
de348fb
07d335e
 
 
 
b9c182b
 
 
 
07d335e
b9c182b
 
 
 
 
07d335e
 
 
b9c182b
07d335e
 
 
e31a853
 
 
07d335e
b9c182b
07d335e
b9c182b
07d335e
 
 
 
b9c182b
07d335e
b9c182b
07d335e
b9c182b
 
 
 
07d335e
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import os

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import PromptTemplate

from openworm_ai.utils.llms import GENERAL_QUERY_PROMPT_TEMPLATE

from openworm_ai.utils.llms import get_llm as get_llm_via_openworm_ai


from openworm_ai.utils.llms import LLM_GPT4
from openworm_ai.utils.llms import LLM_GPT4o
from openworm_ai.utils.llms import LLM_GEMINI_2F
from openworm_ai.utils.llms import LLM_GEMINI_25F
from openworm_ai.utils.llms import LLM_OLLAMA_LLAMA32_1B
from openworm_ai.utils.llms import LLM_OLLAMA_MISTRAL
from openworm_ai.utils.llms import LLM_OLLAMA_OLMO2_7B
from openworm_ai.utils.llms import LLM_COHERE

from openworm_ai.utils.llms import generate_response


OPENAI_LLMS = [LLM_GPT4, LLM_GPT4o]

OLLAMA_LLMS = [LLM_OLLAMA_LLAMA32_1B, LLM_OLLAMA_MISTRAL, LLM_OLLAMA_OLMO2_7B]

GEMINI_LLMS = [LLM_GEMINI_2F, LLM_GEMINI_25F]

OPENWORM_AI_LLMS = GEMINI_LLMS + OPENAI_LLMS + [LLM_COHERE]  # + OLLAMA_LLMS

LLM_LLAMA2 = "LLAMA2"
# LLM_GEMINI = "gemini-1.5-pro"
LLM_CLAUDE2 = "Claude2.1"


PREF_ORDER_LLMS = OPENWORM_AI_LLMS  # + [LLM_LLAMA2, LLM_CLAUDE2]


def get_llamaapi_key():
    llamaapi_key = os.environ.get("LLAMAAPI_KEY")

    return llamaapi_key


def get_anthropic_key():
    anthropic_api_key = os.environ.get["ANTHROPIC_API_KEY"]

    return anthropic_api_key


def get_llm(llm_ver, temperature):
    if llm_ver in OPENWORM_AI_LLMS:
        llm = get_llm_via_openworm_ai(llm_ver, temperature)

    elif llm_ver == LLM_LLAMA2:
        from llamaapi import LlamaAPI
        import asyncio

        # Create a new event loop
        loop = asyncio.new_event_loop()

        # Set the event loop as the current event loop
        asyncio.set_event_loop(loop)

        llama = LlamaAPI(get_llamaapi_key())

        from langchain_experimental.llms import ChatLlamaAPI

        llm = ChatLlamaAPI(client=llama)

    elif llm_ver == LLM_GEMINI:
        k = get_gemini_api_key()
        # print(('1111_%s'%k)[::-1])
        from langchain_google_genai import ChatGoogleGenerativeAI

        llm = ChatGoogleGenerativeAI(model=LLM_GEMINI, google_api_key=k)

    elif llm_ver == LLM_CLAUDE2:
        from langchain_anthropic import AnthropicLLM

        llm = AnthropicLLM(model="claude-2.1")

    return llm


def generate_panel_response(input_text, llm_panelists, llm_panel_chair, temperature):
    responses = {}

    for llm_ver in llm_panelists:
        prompt = PromptTemplate(
            template=GENERAL_QUERY_PROMPT_TEMPLATE, input_variables=["question"]
        )

        llm = get_llm(llm_ver, temperature)

        llm_chain = prompt | llm | StrOutputParser()

        responses[llm_ver] = llm_chain.invoke(input_text)

    panel_chair_prompt = """You are a neuroscientist chairing a panel discussion on the nematode C. elegans. A researcher has asked the following question:
    {question}
    and %i experts on the panel have give their answers. 
    """ % (len(llm_panelists))

    for llm_ver in llm_panelists:
        panel_chair_prompt += """
The panelist named Dr. %s has provided the answer: %s
""" % (
            llm_ver,
            responses[llm_ver],
        )

    panel_chair_prompt += (
        """
Please generate a brief answer to the researcher's question based on their responses, pointing out where there is any inconsistency"""
        + """ in their answers, and using your own knowledge of C. elegans to try to resolve it."""
    )

    print(panel_chair_prompt)

    prompt = PromptTemplate(template=panel_chair_prompt, input_variables=["question"])

    llm = get_llm(llm_panel_chair, temperature)

    llm_chain = prompt | llm | StrOutputParser()

    response_chair = llm_chain.invoke(input_text)

    response = """**%s**: %s""" % (llm_panel_chair, response_chair)

    response += """

-----------------------------------
_Individual responses:_

"""
    for llm_ver in responses:
        response += """
_**%s**:_ _%s_
""" % (
            llm_ver,
            responses[llm_ver].strip().replace("\n", " "),
        )

    return response