codegeex4.py 1.3 KB

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