Spaces:
Sleeping
Sleeping
File size: 1,209 Bytes
bdfad5e |
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 |
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
os.environ['OPENAI_API_KEY'] = "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"
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)
print(response)
else:
response = "Please add a request url first"
return response
|