24 呼叫已建置的代理程式

您可以從生產環境應用程式呼叫代理程式的端點 URL。

無論用於呼叫代理程式端點的程式設計介面為何,都必須向 OCI 進行認證,並具有相關權限。如果是服務人員端點,則呼叫者必須至少具備該服務人員端點的 USE 權限。

端點 URI 會記錄在代理程式 UI 的「詳細資訊」頁籤中。您可以將該端點 URI 複製到您的程式碼中,以呼叫代理程式。


以醒目提示「端點 URI」欄位開啟之代理程式的「詳細資訊」頁面

呼叫端點 URI 的方法

您可以透過不同的工具、SDK 和 CLI 來呼叫代理程式端點 URI。

下列方法可讓您在 Oracle AI Data Platform Workbench 代理程式中呼叫您的端點 URI。

使用 OCI CLI 呼叫

提供的範例示範如何使用 OCI CLI,並使用安全權杖進行認證。將 <your-agent-flow-endpoint-uri> 取代為您的代理程式端點 URI,將 <security_token> 取代為您的 OCI 安全權杖。
oci raw-request
--http-method POST
--target-uri <your-agent-flow-endpoint-uri>
--request-body '{"query":"Tell me about the Ryder Cup in 1985"}'
--auth <security_token>

使用 Python 要求函式庫呼叫

您可以使用 OCI Python SDK 建立簽署者以使用 OCI 進行認證。Python 要求程式庫可用來將要求張貼至代理程式端點 URI 並傳回回應。下列範例示範如何透過 OCI 組態和私密金鑰檔案使用您的使用者主體:
import oci import requests import json import uuid from contextlib import closing from requests import Request, Response
class AuthHelper: """ AuthHelper allows creating an OCI signer with either API key or security_token (which are short term sessions) """ def init(self, oci_profile: str, use_security_token:bool = True): config = oci.config.from_file(file_location="/Volumes/jr/default/misc/config",profile_name=oci_profile) if use_security_token: with open(config["security_token_file"], 'r') as f: token = f.read() private_key = oci.signer.load_private_key_from_file(config["key_file"])
 self.signer = oci.auth.signers.SecurityTokenSigner(token, private_key) else: self.signer = oci.signer.Signer( tenancy=config["tenancy"], user=config["user"], fingerprint=config["fingerprint"], private_key_file_location=config["key_file"], #pass_phrase=config.get("pass_phrase"), #private_key_content=config.get("key_content") )
@property
def Signer(self):
    return self.signer
 
class MyRawJsonRpcClient: """ Simple class using requests lib to post JSON to chat endpoint using OCI signing """ def init(self, chat_url:str, oci_profile: str, sessionKey:str, use_security_token:bool = True): self.authhelper = AuthHelper(oci_profile=oci_profile, use_security_token=use_security_token) self.authsigner = self.authhelper.Signer self.chat_url = chat_url self.sessionKey = sessionKey
def send(self, input:str) -> Response:
    body = {
        "isStreamEnabled" : False,
        "sessionKey" : self.sessionKey,
        "trace" : False,
        "input" :[{
            "role":"User",
            "content":[{
                "type" : "INPUT_TEXT",
                "text" : input                   
            }]
        }]
    }

    response:Request = requests.post(
        url =self.chat_url,
        params = None,
        auth = self.authsigner,
        json=body,
        headers={}
    )
    return response
您可以透過建立 MyRawJsonRPCClient 類別並提供 OCI 設定檔值 (在 OCI 組態檔中找到)、代理程式端點 URI (chat_url) 和 sessionKey 來呼叫代理程式端點 URI。您可以任意提供 sessionKeysessionKey 是代理程式之使用者階段作業的唯一 ID。如果您持續重複使用相同的 sessionKey,使用者訊息和代理程式回應會附加至相同的保留。
client = MyRawJsonRpcClient(chat_url="<your-agent-flow-endpoint-uri>",
   oci_profile = "DEFAULT",
   sessionKey= “<your-session-key>”,
   use_security_token = False )
您也可以提供使用者訊息,並使用從屬端將訊息傳送至代理程式端點 URI:
user_input = f"Hello, tell me a good dad joke."
r = client.send(input = user_input)
response_json = r.json()

透過 APEX

您可以使用 AI Data Platform Workbench 範例 Github 儲存庫中的程式碼範例。此範例會逐步引導您從 APEX 應用程式呼叫代理程式部署端點。

透過 Streamlit

您可以使用 AI Data Platform Workbench 範例 Github 儲存庫中的程式碼範例。此範例會逐步引導您從 Streamlit 應用程式呼叫代理程式部署端點。

最佳作法 - 非同步與非同步回應

建議您使用非同步回應撰寫您的從屬端程式碼。舉例而言:

import httpx 
 
async def fetch_data(): 
    async with httpx.AsyncClient() as client: 
        response = await client.get(URL) 
        return response.json()