Advertisement
xpppppppaicyber

ai

Sep 27th, 2024
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.97 KB | None | 0 0
  1. function getResponseFromGroq($prompt) {
  2.     $GROQ_API_KEY = "isi api key";
  3.     $url = "https://api.groq.com/openai/v1/chat/completions";
  4.     $headers = ["Content-Type: application/json","Authorization: Bearer " . $GROQ_API_KEY];
  5.  
  6.     $data = "";
  7.     if (containsWord($prompt, "land")) {
  8.         preg_match('/\d+/', $prompt, $matches);
  9.         if (isset($matches[0])) {
  10.             $data = landScreener($matches[0]);
  11.             echo "$data";
  12.         } else {
  13.             $data = "No number found";
  14.         }
  15.        
  16.     }
  17.  
  18.  
  19.     $data = [
  20.         "messages" => [
  21.             [
  22.                 "role" => "system",
  23.                 "content" => "Your name is Xy, only answer with data, if you being ask about land data / information, you can answer with ". $data. " , you can wrap it with ```\n  ```"
  24.             ],
  25.             [
  26.                 "role" => "user",
  27.                 "content" => $prompt
  28.             ]
  29.         ],
  30.         "model" => "llama3-70b-8192",
  31.         "temperature" => 0.5,
  32.         "max_tokens" => 8192,
  33.         "top_p" => 1,
  34.         "stream" => false,
  35.         "stop" => null
  36.     ];
  37.     $options = [
  38.         CURLOPT_URL            => $url,
  39.         CURLOPT_CUSTOMREQUEST  => "POST",
  40.         CURLOPT_POSTFIELDS     => json_encode($data),
  41.         CURLOPT_RETURNTRANSFER => true,
  42.         CURLOPT_HTTPHEADER     => $headers
  43.     ];
  44.  
  45.     $curl = curl_init();
  46.     curl_setopt_array($curl, $options);
  47.     $response = curl_exec($curl);
  48.     curl_close($curl);
  49.     $result = json_decode($response, true);
  50.     $msg = $result['choices'][0]['message']['content'];
  51.     $ptoken = $result['usage']['prompt_tokens'];
  52.     $rtoken = $result['usage']['completion_tokens'];
  53.     $ttoken = $result['usage']['total_tokens'];
  54.     $speed = $result['usage']['total_time'];
  55.    
  56.     $final = array("$msg","`Prompt tokens: " . $ptoken . "\nCompletion tokens: " . $rtoken . "\nTotal tokens: " . $ttoken . "\nSpeed: " . $speed . " seconds` | XC");
  57.     return $final;
  58.    
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement