From 8434cc68130853e17592bb2588552806305884d3 Mon Sep 17 00:00:00 2001 From: reibs Date: Mon, 25 Nov 2024 15:39:26 -0800 Subject: [PATCH] added fastapi test --- tests/core_manual_tests/api_server/main.py | 40 ++++++++++++++++++++ tests/core_manual_tests/api_server/server.py | 39 ------------------- 2 files changed, 40 insertions(+), 39 deletions(-) create mode 100644 tests/core_manual_tests/api_server/main.py delete mode 100644 tests/core_manual_tests/api_server/server.py diff --git a/tests/core_manual_tests/api_server/main.py b/tests/core_manual_tests/api_server/main.py new file mode 100644 index 000000000..e4df770d4 --- /dev/null +++ b/tests/core_manual_tests/api_server/main.py @@ -0,0 +1,40 @@ +import agentops +from fastapi import FastAPI +import uvicorn +from dotenv import load_dotenv +from agentops import record_tool +from openai import OpenAI +import time + +load_dotenv() + +openai = OpenAI() +agentops.init(auto_start_session=False) +app = FastAPI() + + +@app.get("/completion") +def completion(): + start_time = time.time() + + session = agentops.start_session(tags=["api-server-test"]) + + @record_tool(tool_name="foo") + def foo(x: str): + print(x) + + foo("Hello") + + session.end_session(end_state="Success") + + end_time = time.time() + execution_time = end_time - start_time + + return { + "response": 'Done', + "execution_time_seconds": execution_time + } + + +if __name__ == "__main__": + uvicorn.run("main:app", host="127.0.0.1", port=8000, reload=True) diff --git a/tests/core_manual_tests/api_server/server.py b/tests/core_manual_tests/api_server/server.py deleted file mode 100644 index 5ae5e1ae2..000000000 --- a/tests/core_manual_tests/api_server/server.py +++ /dev/null @@ -1,39 +0,0 @@ -import agentops -from fastapi import FastAPI -import uvicorn -from dotenv import load_dotenv -from agentops import ActionEvent -from openai import OpenAI - -load_dotenv() - -openai = OpenAI() -agentops.init() -app = FastAPI() - - -@app.get("/completion") -def completion(): - session = agentops.start_session(tags=["api-server-test"]) - - messages = [{"role": "user", "content": "Hello"}] - response = session.patch(openai.chat.completions.create)( - model="gpt-3.5-turbo", - messages=messages, - temperature=0.5, - ) - - session.record( - ActionEvent( - action_type="Agent says hello", - params=messages, - returns=str(response.choices[0].message.content), - ), - ) - - session.end_session(end_state="Success") - - return {"response": response} - - -uvicorn.run(app, host="0.0.0.0", port=9696)