File size: 6,207 Bytes
30596e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5b988a
 
 
 
 
 
 
 
 
30596e4
 
 
 
519bb78
b5b988a
30596e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519bb78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
30596e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b5b988a
 
 
 
fcbd089
b5b988a
30596e4
 
 
 
 
 
 
 
 
 
 
 
 
fcbd089
 
 
 
 
 
519bb78
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fcbd089
519bb78
30596e4
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
# LangChain supports many other chat models. Here, we're using Ollama
from langchain_community.chat_models import ChatOllama
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain.tools.retriever import create_retriever_tool
from langchain_community.utilities import SerpAPIWrapper
from langchain.retrievers import ArxivRetriever
from langchain_core.tools import Tool
from langchain import hub
from langchain.agents import AgentExecutor, load_tools
from langchain.agents.format_scratchpad import format_log_to_str
from langchain.agents.output_parsers import (
    ReActJsonSingleInputOutputParser,
)
from langchain.tools.render import render_text_description
import os

import dotenv

dotenv.load_dotenv()

 
OLLMA_BASE_URL = os.getenv("OLLMA_BASE_URL")


# supports many more optional parameters. Hover on your `ChatOllama(...)`
# class to view the latest available supported parameters
llm = ChatOllama(
    model="mistral:instruct",
    base_url= OLLMA_BASE_URL
    )
prompt = ChatPromptTemplate.from_template("Tell me a short joke about {topic}")

# using LangChain Expressive Language chain syntax
# learn more about the LCEL on
# https://python.langchain.com/docs/expression_language/why
chain = prompt | llm | StrOutputParser()

# for brevity, response is printed in terminal
# You can use LangServe to deploy your application for
# production
print(chain.invoke({"topic": "Space travel"}))

retriever = ArxivRetriever(load_max_docs=2)

# Import things that are needed generically
from langchain.pydantic_v1 import BaseModel, Field
from langchain.tools import BaseTool, StructuredTool, tool


global all_sources

# @tool
# def search(query: str) -> str:
#     """Look up things online."""
#     # return "LangChain"
#     data = retriever.invoke(query)
#     meta_data = [i.metadata for i in data]
#     # meta_data += all_sources
#     # all_sources += meta_data
#     all_sources += meta_data
#     # all_sources = []
#     return meta_data

from typing import List, Dict
from datetime import datetime

def format_info_list(info_list: List[Dict[str, str]]) -> str:
    """
    Format a list of dictionaries containing information into a single string.

    Args:
        info_list (List[Dict[str, str]]): A list of dictionaries containing information.

    Returns:
        str: A formatted string containing the information from the list.
    """
    formatted_strings = []
    for info_dict in info_list:
        formatted_string = "|"
        for key, value in info_dict.items():
            if isinstance(value, datetime.date):
                value = value.strftime('%Y-%m-%d')
            formatted_string += f"'{key}': '{value}', "
        formatted_string = formatted_string.rstrip(', ') + "|"
        formatted_strings.append(formatted_string)
    return '\n'.join(formatted_strings)
    
@tool
def search(query: str) -> str:
    """Look up things online."""
    # return "LangChain"
    global all_sources
    data = retriever.invoke(query)
    meta_data = [i.metadata for i in data]
    # meta_data += all_sources
    # all_sources += meta_data
    all_sources += meta_data
    
    # formatted_info = format_info(entry_id, published, title, authors)
    
    # formatted_info = format_info_list(all_sources)
    
    return meta_data.__str__()
    
    # all_sources = []
    # return meta_data

tools = [search]

# tools = [
#     create_retriever_tool(
#     retriever,
#     "search arxiv's database for",
#     "Use this to recomend the user a paper to read Unless stated please choose the most recent models",
#     # "Searches and returns excerpts from the 2022 State of the Union.",
#     ),

#     Tool(
#         name="SerpAPI",
#         description="A low-cost Google Search API. Useful for when you need to answer questions about current events. Input should be a search query.",
#         func=SerpAPIWrapper().run,
#     )

# ]



prompt = hub.pull("hwchase17/react-json")
prompt = prompt.partial(
    tools=render_text_description(tools),
    tool_names=", ".join([t.name for t in tools]),
)

chat_model = llm
# define the agent
chat_model_with_stop = chat_model.bind(stop=["\nObservation"])
agent = (
    {
        "input": lambda x: x["input"],
        "agent_scratchpad": lambda x: format_log_to_str(x["intermediate_steps"]),
    }
    | prompt
    | chat_model_with_stop
    | ReActJsonSingleInputOutputParser()
)

# instantiate AgentExecutor
agent_executor = AgentExecutor(
    agent=agent, 
    tools=tools, 
    verbose=True,
    handle_parsing_errors=True #prevents error
    )

# agent_executor.invoke(
#     {
#         "input": "Who is the current holder of the speed skating world record on 500 meters? What is her current age raised to the 0.43 power?"
#     }
# )

# agent_executor.invoke(
#     {
#         "input": "what are large language models and why are they so expensive to run?"
#     }
# )

# agent_executor.invoke(
#     {
#         "input": "How to generate videos from images using state of the art macchine learning models"
#     }
# )

# class AgentSample:
    
    
#     def __init__(self, agent_executor_object,*args, **kwargs):
#         self.agent_executor_object = agent_executor_object
#         self.meta_data = []
        
#     def sample_invokex
        
        
    

if __name__ == "__main__":
    
    # global variable for collecting sources
    all_sources =  []    

    input = agent_executor.invoke(
        {
            "input": "How to generate videos from images using state of the art macchine learning models; Using the axriv retriever  " +
            "add the urls of the papers used in the final answer using the metadata from the retriever"
            # f"Please prioritize the newest papers this is the current data {get_current_date()}"
        }
    )
    
    x = 0

    input_1 = agent_executor.invoke(
        {
            "input": "I am looking for a text to 3d model; Using the axriv retriever  " +
            "add the urls of the papers used in the final answer using the metadata from the retriever"
            # f"Please prioritize the newest papers this is the current data {get_current_date()}"
        }
    )

    x = 0