codegeex4.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import json
  2. import requests
  3. URL = "" # the url you deploy codegeex service
  4. def codegeex4(prompt, temperature=0.8, top_p=0.8):
  5. url = URL
  6. headers = {"Content-Type": "application/json"}
  7. data = {
  8. "inputs": prompt,
  9. "parameters": {
  10. "best_of": 1,
  11. "do_sample": True,
  12. "max_new_tokens": 4012,
  13. "temperature": temperature,
  14. "top_p": top_p,
  15. "stop": ["<|endoftext|>", "<|user|>", "<|observation|>", "<|assistant|>"],
  16. },
  17. }
  18. response = requests.post(url, json=data, headers=headers, verify=False, stream=True)
  19. if response.status_code == 200:
  20. for line in response.iter_lines():
  21. if line:
  22. decoded_line = line.decode("utf-8").replace("data:", "").strip()
  23. if decoded_line:
  24. try:
  25. content = json.loads(decoded_line)
  26. token_text = content.get("token", {}).get("text", "")
  27. if "<|endoftext|>" in token_text:
  28. break
  29. yield token_text
  30. except json.JSONDecodeError:
  31. continue
  32. else:
  33. print("请求失败:", response.status_code)