Spaces:
Sleeping
Sleeping
import os | |
import requests | |
import yaml | |
from langchain.agents import ( | |
create_json_agent | |
) | |
from langchain.agents.agent_toolkits import JsonToolkit | |
from langchain.chat_models import ChatOpenAI | |
from langchain.tools.json.tool import JsonSpec | |
llm = ChatOpenAI(model_name="gpt-4", temperature=0.1) | |
agent = None | |
"""Call the url and use json response to create a yaml file""" | |
def index_url(url): | |
global agent | |
response = requests.get(url) | |
json_response = response.json() | |
# create yaml from json response | |
yaml_string = yaml.dump(json_response) | |
data = yaml.load(yaml_string, Loader=yaml.FullLoader) | |
json_spec = JsonSpec(dict_=data, max_value_length=4000) | |
json_toolkit = JsonToolkit(spec=json_spec) | |
agent = create_json_agent( | |
llm=llm, | |
toolkit=json_toolkit, | |
verbose=True | |
) | |
return data | |
"""Use the agent to call method run using the text as input""" | |
def chat(question): | |
global agent | |
if agent is not None: | |
response = agent.run(question) | |
else: | |
response = "Please add a request url first" | |
return response | |